1 /* $Id: decmpeg2.c,v 1.12 2005/03/03 16:30:42 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.fr/>.
5 It may be used under the terms of the GNU General Public License. */
9 #include "mpeg2dec/mpeg2.h"
11 /* Cadence tracking */
12 #ifndef PIC_FLAG_REPEAT_FIRST_FIELD
13 #define PIC_FLAG_REPEAT_FIRST_FIELD 256
15 #define TOP_FIRST PIC_FLAG_TOP_FIELD_FIRST
16 #define PROGRESSIVE PIC_FLAG_PROGRESSIVE_FRAME
17 #define COMPOSITE PIC_FLAG_COMPOSITE_DISPLAY
18 #define SKIP PIC_FLAG_SKIP
19 #define TAGS PIC_FLAG_TAGS
20 #define REPEAT_FIRST PIC_FLAG_REPEAT_FIRST_FIELD
21 #define COMPOSITE_MASK PIC_MASK_COMPOSITE_DISPLAY
29 /**********************************************************************
31 *********************************************************************/
32 typedef struct hb_libmpeg2_s
34 mpeg2dec_t * libmpeg2;
35 const mpeg2_info_t * info;
42 int got_iframe; /* set when we get our first iframe */
43 int look_for_iframe; /* need an iframe to add chap break */
44 int look_for_break; /* need gop start to add chap break */
45 uint32_t nframes; /* number of frames we've decoded */
49 hb_list_t * list_subtitle;
52 /**********************************************************************
54 **********************************************************************
56 *********************************************************************/
57 static hb_libmpeg2_t * hb_libmpeg2_init()
59 hb_libmpeg2_t * m = calloc( sizeof( hb_libmpeg2_t ), 1 );
61 m->libmpeg2 = mpeg2_init();
62 m->info = mpeg2_info( m->libmpeg2 );
68 static void hb_mpeg2_cc( hb_libmpeg2_t * m, uint8_t *cc_block )
70 uint8_t cc_valid = (*cc_block & 4) >>2;
71 uint8_t cc_type = *cc_block & 3;
74 hb_subtitle_t * subtitle;
79 * Ignore CC decoding during scanning.
84 if (cc_valid || cc_type==3)
90 for (i = 0; i < hb_list_count( m->list_subtitle ); i++)
92 subtitle = hb_list_item( m->list_subtitle, i );
93 cc_buf = hb_buffer_init( 2 );
96 cc_buf->start = m->last_pts;
97 memcpy( cc_buf->data, cc_block+1, 2 );
98 hb_fifo_push( subtitle->fifo_in, cc_buf );
104 //process608( cc_block+1, 2, &m->cc608 );
116 //do_708 ((const unsigned char *) temp, 4);
125 hb_log("Ignoring invalid CC block");
129 static hb_buffer_t *hb_copy_frame( hb_job_t *job, int width, int height,
130 uint8_t* y, uint8_t *u, uint8_t *v )
132 int dst_w = width, dst_h = height;
135 dst_w = job->title->width;
136 dst_h = job->title->height;
138 int dst_wh = dst_w * dst_h;
139 hb_buffer_t *buf = hb_video_buffer_init( dst_w, dst_h );
141 if ( dst_w != width || dst_h != height )
143 // we're encoding and the frame dimensions don't match the title dimensions -
144 // rescale & matte Y, U, V into our output buf.
146 avpicture_alloc(&in, PIX_FMT_YUV420P, width, height );
147 avpicture_alloc(&out, PIX_FMT_YUV420P, dst_w, dst_h );
149 int src_wh = width * height;
150 memcpy( in.data[0], y, src_wh );
151 memcpy( in.data[1], u, src_wh >> 2 );
152 memcpy( in.data[2], v, src_wh >> 2 );
153 struct SwsContext *context = sws_getContext( width, height, PIX_FMT_YUV420P,
154 dst_w, dst_h, PIX_FMT_YUV420P,
155 SWS_LANCZOS|SWS_ACCURATE_RND,
157 sws_scale( context, in.data, in.linesize, 0, height, out.data, out.linesize );
158 sws_freeContext( context );
160 uint8_t *data = buf->data;
161 memcpy( data, out.data[0], dst_wh );
163 // U & V planes are 1/4 the size of Y plane.
165 memcpy( data, out.data[1], dst_wh );
167 memcpy( data, out.data[2], dst_wh );
169 avpicture_free( &out );
170 avpicture_free( &in );
174 // we're scanning or the frame dimensions match the title's dimensions
175 // so we can do a straight copy.
176 uint8_t *data = buf->data;
177 memcpy( data, y, dst_wh );
179 // U & V planes are 1/4 the size of Y plane.
181 memcpy( data, u, dst_wh );
183 memcpy( data, v, dst_wh );
188 /**********************************************************************
190 **********************************************************************
192 *********************************************************************/
193 static int hb_libmpeg2_decode( hb_libmpeg2_t * m, hb_buffer_t * buf_es,
194 hb_list_t * list_raw )
202 if( buf_es->start > -1 )
204 mpeg2_tag_picture( m->libmpeg2, buf_es->start >> 32,
205 buf_es->start & 0xFFFFFFFF );
207 mpeg2_buffer( m->libmpeg2, buf_es->data,
208 buf_es->data + buf_es->size );
213 state = mpeg2_parse( m->libmpeg2 );
214 if( state == STATE_BUFFER )
216 /* Require some more data */
219 else if( state == STATE_SEQUENCE )
221 if( !( m->width && m->height && m->rate ) )
223 m->width = m->info->sequence->width;
224 m->height = m->info->sequence->height;
225 m->rate = m->info->sequence->frame_period;
226 if ( m->aspect_ratio <= 0 && m->height &&
227 m->info->sequence->pixel_height )
229 /* mpeg2_parse doesn't store the aspect ratio. Instead
230 * it keeps the pixel width & height that would cause
231 * the storage width & height to come out in the correct
232 * aspect ratio. Convert these back to aspect ratio.
234 double ar_numer = m->width * m->info->sequence->pixel_width;
235 double ar_denom = m->height * m->info->sequence->pixel_height;
236 m->aspect_ratio = ar_numer / ar_denom;
240 else if( state == STATE_GOP && m->look_for_break)
242 // we were looking for a gop to add a chapter break - we found it
243 // so now start looking for an iframe.
244 m->look_for_iframe = m->look_for_break;
245 m->look_for_break = 0;
247 else if( ( state == STATE_SLICE || state == STATE_END ) &&
248 m->info->display_fbuf )
250 if( ( m->info->display_picture->flags &
251 PIC_MASK_CODING_TYPE ) == PIC_FLAG_CODING_TYPE_I )
253 // we got an iframe so we can start decoding video now
259 buf = hb_copy_frame( m->job, m->info->sequence->width,
260 m->info->sequence->height,
261 m->info->display_fbuf->buf[0],
262 m->info->display_fbuf->buf[1],
263 m->info->display_fbuf->buf[2] );
264 buf->sequence = buf_es->sequence;
266 if( m->info->display_picture->flags & PIC_FLAG_TAGS )
269 ( (uint64_t) m->info->display_picture->tag << 32 ) |
270 ( (uint64_t) m->info->display_picture->tag2 );
272 else if( m->last_pts > -1 )
274 /* For some reason nb_fields is sometimes 1 while it
276 buf->start = m->last_pts +
277 MAX( 2, m->info->display_picture->nb_fields ) *
278 m->info->sequence->frame_period / 600;
284 m->last_pts = buf->start;
286 if( m->look_for_iframe && ( m->info->display_picture->flags &
287 PIC_MASK_CODING_TYPE ) == PIC_FLAG_CODING_TYPE_I )
289 // we were waiting for an iframe to insert a chapter mark
291 buf->new_chap = m->look_for_iframe;
292 m->look_for_iframe = 0;
293 const char *chap_name = "";
294 if ( m->job && buf->new_chap > 0 &&
295 hb_list_item( m->job->title->list_chapter,
296 buf->new_chap - 1 ) )
298 hb_chapter_t * c = hb_list_item( m->job->title->list_chapter,
300 chap_name = c->title;
302 hb_log( "mpeg2: \"%s\" (%d) at frame %u time %lld",
303 chap_name, buf->new_chap, m->nframes, buf->start );
304 } else if ( m->nframes == 0 && m->job &&
305 hb_list_item( m->job->title->list_chapter,
306 m->job->chapter_start - 1 ) )
308 hb_chapter_t * c = hb_list_item( m->job->title->list_chapter,
309 m->job->chapter_start - 1 );
310 hb_log( "mpeg2: \"%s\" (%d) at frame %u time %lld", c->title,
311 m->job->chapter_start, m->nframes, buf->start );
315 m->flag = m->info->display_picture->flags;
317 /* Uncomment this block to see frame-by-frame picture flags, as the video encodes.
318 hb_log("***** MPEG 2 Picture Info for PTS %lld *****", buf->start);
319 if( m->flag & TOP_FIRST )
320 hb_log("MPEG2 Flag: Top field first");
321 if( m->flag & PROGRESSIVE )
322 hb_log("MPEG2 Flag: Progressive");
323 if( m->flag & COMPOSITE )
324 hb_log("MPEG2 Flag: Composite");
326 hb_log("MPEG2 Flag: Skip!");
328 hb_log("MPEG2 Flag: TAGS");
329 if(fm->lag & REPEAT_FIRST )
330 hb_log("MPEG2 Flag: Repeat first field");
331 if( m->flag & COMPOSITE_MASK )
332 hb_log("MPEG2 Flag: Composite mask");
333 hb_log("fields: %d", m->info->display_picture->nb_fields);
335 /* Rotate the cadence tracking. */
337 for(i=11; i > 0; i--)
339 m->cadence[i] = m->cadence[i-1];
342 if ( !(m->flag & PROGRESSIVE) && !(m->flag & TOP_FIRST) )
344 /* Not progressive, not top first...
345 That means it's probably bottom
346 first, 2 fields displayed.
348 //hb_log("MPEG2 Flag: Bottom field first, 2 fields displayed.");
351 else if ( !(m->flag & PROGRESSIVE) && (m->flag & TOP_FIRST) )
353 /* Not progressive, top is first,
354 Two fields displayed.
356 //hb_log("MPEG2 Flag: Top field first, 2 fields displayed.");
359 else if ( (m->flag & PROGRESSIVE) && !(m->flag & TOP_FIRST) && !( m->flag & REPEAT_FIRST ) )
361 /* Progressive, but noting else.
362 That means Bottom first,
365 //hb_log("MPEG2 Flag: Progressive. Bottom field first, 2 fields displayed.");
366 m->cadence[0] = BT_PROG;
368 else if ( (m->flag & PROGRESSIVE) && !(m->flag & TOP_FIRST) && ( m->flag & REPEAT_FIRST ) )
370 /* Progressive, and repeat. .
371 That means Bottom first,
374 //hb_log("MPEG2 Flag: Progressive repeat. Bottom field first, 3 fields displayed.");
375 m->cadence[0] = BTB_PROG;
377 else if ( (m->flag & PROGRESSIVE) && (m->flag & TOP_FIRST) && !( m->flag & REPEAT_FIRST ) )
379 /* Progressive, top first.
380 That means top first,
383 //hb_log("MPEG2 Flag: Progressive. Top field first, 2 fields displayed.");
384 m->cadence[0] = TB_PROG;
386 else if ( (m->flag & PROGRESSIVE) && (m->flag & TOP_FIRST) && ( m->flag & REPEAT_FIRST ) )
388 /* Progressive, top, repeat.
389 That means top first,
392 //hb_log("MPEG2 Flag: Progressive repeat. Top field first, 3 fields displayed.");
393 m->cadence[0] = TBT_PROG;
396 if ( (m->cadence[2] <= TB) && (m->cadence[1] <= TB) && (m->cadence[0] > TB) && (m->cadence[11]) )
397 hb_log("%fs: Video -> Film", (float)buf->start / 90000);
398 if ( (m->cadence[2] > TB) && (m->cadence[1] <= TB) && (m->cadence[0] <= TB) && (m->cadence[11]) )
399 hb_log("%fs: Film -> Video", (float)buf->start / 90000);
401 /* Store picture flags for later use by filters */
402 buf->flags = m->info->display_picture->flags;
404 hb_list_add( list_raw, buf );
407 else if( state == STATE_INVALID )
409 mpeg2_reset( m->libmpeg2, 0 );
413 * Look for Closed Captions if scanning (!job) or if closed captions have been requested.
415 * Send them on to the closed caption decoder if requested and found.
417 if( ( !m->job || hb_list_count( m->list_subtitle) ) &&
418 ( m->info->user_data_len != 0 &&
419 m->info->user_data[0] == 0x43 &&
420 m->info->user_data[1] == 0x43 ) )
423 const uint8_t *header = &m->info->user_data[4];
424 uint8_t pattern=header[0] & 0x80;
425 int field1packet = 0; /* expect Field 1 first */
427 field1packet=1; /* expect Field 1 second */
428 int capcount=(header[0] & 0x1e) / 2;
432 * Add closed captions to the title if we are scanning (no job).
434 * Just because we don't add this doesn't mean that there aren't any when
435 * we scan, just that noone said anything. So you should be able to add
436 * closed captions some other way (See decmpeg2Init() for alternative
437 * approach of assuming that there are always CC, which is probably
438 * safer - however I want to leave the autodetect in here for now to
441 if( !m->job && m->title )
443 hb_subtitle_t * subtitle;
447 for( i = 0; i < hb_list_count( m->title->list_subtitle ); i++ )
449 subtitle = hb_list_item( m->title->list_subtitle, i);
451 * Let's call them 608 subs for now even if they aren't, since they
452 * are the only types we grok.
454 if( subtitle && subtitle->source == CC608SUB )
463 subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
466 snprintf( subtitle->lang, sizeof( subtitle->lang ), "Closed Captions");
468 * The language of the subtitles will be the same as the first audio
469 * track, i.e. the same as the video.
471 hb_audio_t *audio = hb_list_item( m->title->list_audio, 0 );
474 snprintf( subtitle->iso639_2, sizeof( subtitle->iso639_2 ),
475 audio->config.lang.iso639_2);
477 snprintf( subtitle->iso639_2, sizeof( subtitle->iso639_2 ), "und");
479 subtitle->format = TEXTSUB;
480 subtitle->source = CC608SUB;
481 subtitle->config.dest = PASSTHRUSUB;
484 hb_list_add( m->title->list_subtitle, subtitle );
488 for( i=0; i<capcount; i++ )
497 /* Field 1 and 2 data can be in either order,
498 with marker bytes of \xff and \xfe
499 Since markers can be repeated, use pattern as well */
500 if( data[0] == 0xff && j == field1packet )
502 data[0] = 0x04; // Field 1
506 data[0] = 0x05; // Field 2
508 hb_mpeg2_cc( m, data );
511 // Deal with extra closed captions some DVD have.
512 while( header[0]==0xfe || header[0]==0xff )
521 /* Field 1 and 2 data can be in either order,
522 with marker bytes of \xff and \xfe
523 Since markers can be repeated, use pattern as well */
524 if( data[0] == 0xff && j == field1packet )
526 data[0] = 0x04; // Field 1
530 data[0] = 0x05; // Field 2
532 hb_mpeg2_cc( m, data );
540 /**********************************************************************
542 **********************************************************************
544 *********************************************************************/
545 static void hb_libmpeg2_close( hb_libmpeg2_t ** _m )
547 hb_libmpeg2_t * m = *_m;
549 mpeg2_close( m->libmpeg2 );
555 /**********************************************************************
556 * The decmpeg2 work object
557 **********************************************************************
559 *********************************************************************/
560 struct hb_work_private_s
562 hb_libmpeg2_t * libmpeg2;
566 /**********************************************************************
567 * hb_work_decmpeg2_init
568 **********************************************************************
570 *********************************************************************/
571 static int decmpeg2Init( hb_work_object_t * w, hb_job_t * job )
573 hb_work_private_t * pv;
575 pv = calloc( 1, sizeof( hb_work_private_t ) );
576 w->private_data = pv;
578 pv->libmpeg2 = hb_libmpeg2_init();
579 pv->list = hb_list_init();
581 pv->libmpeg2->job = job;
583 if( job && job->title ) {
584 pv->libmpeg2->title = job->title;
588 * If not scanning, then are we supposed to extract Closed Captions
589 * and send them to the decoder?
591 pv->libmpeg2->list_subtitle = hb_list_init();
594 hb_subtitle_t * subtitle;
597 for( i = 0; i < hb_list_count( job->list_subtitle ); i++ )
599 subtitle = hb_list_item( job->list_subtitle, i);
600 if( subtitle && subtitle->source == CC608SUB )
602 hb_list_add(pv->libmpeg2->list_subtitle, subtitle);
611 /**********************************************************************
613 **********************************************************************
615 *********************************************************************/
616 static int decmpeg2Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
617 hb_buffer_t ** buf_out )
619 hb_work_private_t * pv = w->private_data;
620 hb_buffer_t * buf, * last = NULL;
621 int status = HB_WORK_OK;
623 if( w->title && pv && pv->libmpeg2 && !pv->libmpeg2->title ) {
624 pv->libmpeg2->title = w->title;
627 // The reader found a chapter break. Remove it from the input
628 // stream. If we're reading (as opposed to scanning) start looking
629 // for the next GOP start since that's where the chapter begins.
630 if( (*buf_in)->new_chap )
632 if ( pv->libmpeg2->job )
634 pv->libmpeg2->look_for_break = (*buf_in)->new_chap;
636 (*buf_in)->new_chap = 0;
639 hb_libmpeg2_decode( pv->libmpeg2, *buf_in, pv->list );
641 /* if we got an empty buffer signaling end-of-stream send it downstream */
642 if ( (*buf_in)->size == 0 )
646 hb_list_add( pv->list, *buf_in );
648 status = HB_WORK_DONE;
650 * Let the Closed Captions know that it is the end of the data.
652 for (i = 0; i < hb_list_count( pv->libmpeg2->list_subtitle ); i++)
654 hb_subtitle_t * subtitle;
655 hb_buffer_t *buf_eof = hb_buffer_init( 0 );
657 subtitle = hb_list_item( pv->libmpeg2->list_subtitle, i );
660 hb_fifo_push( subtitle->fifo_in, buf_eof );
666 while( ( buf = hb_list_item( pv->list, 0 ) ) )
668 hb_list_rem( pv->list, buf );
684 /**********************************************************************
686 **********************************************************************
688 *********************************************************************/
689 static void decmpeg2Close( hb_work_object_t * w )
691 hb_work_private_t * pv = w->private_data;
693 // don't log during scan
694 if ( pv->libmpeg2->job )
696 hb_log( "mpeg2 done: %d frames", pv->libmpeg2->nframes );
698 hb_list_close( &pv->list );
699 hb_list_close( &pv->libmpeg2->list_subtitle );
700 hb_libmpeg2_close( &pv->libmpeg2 );
704 static int decmpeg2Info( hb_work_object_t *w, hb_work_info_t *info )
706 hb_work_private_t *pv = w->private_data;
708 memset( info, 0, sizeof(*info) );
710 if ( pv && pv->libmpeg2 && pv->libmpeg2->info && pv->libmpeg2->info->sequence )
712 hb_libmpeg2_t *m = pv->libmpeg2;
714 info->width = m->width;
715 info->height = m->height;
716 info->pixel_aspect_width = m->info->sequence->pixel_width;
717 info->pixel_aspect_height = m->info->sequence->pixel_height;
718 info->aspect = m->aspect_ratio;
720 // if the frame is progressive & NTSC DVD height report it as 23.976 FPS
721 // so that scan can autodetect NTSC film
722 info->rate = 27000000;
723 info->rate_base = ( m->info->display_fbuf && m->info->display_picture &&
724 (m->info->display_picture->flags & PROGRESSIVE) &&
725 (m->height == 480 ) ) ? 1126125 : m->rate;
727 info->bitrate = m->info->sequence->byte_rate * 8;
728 info->profile = m->info->sequence->profile_level_id >> 4;
729 info->level = m->info->sequence->profile_level_id & 0xf;
730 info->name = "mpeg2";
736 hb_work_object_t hb_decmpeg2 =
739 "MPEG-2 decoder (libmpeg2)",