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"
10 #include "libavcodec/avcodec.h"
11 #include "libswscale/swscale.h"
13 /* Cadence tracking */
14 #ifndef PIC_FLAG_REPEAT_FIRST_FIELD
15 #define PIC_FLAG_REPEAT_FIRST_FIELD 256
17 #define TOP_FIRST PIC_FLAG_TOP_FIELD_FIRST
18 #define PROGRESSIVE PIC_FLAG_PROGRESSIVE_FRAME
19 #define COMPOSITE PIC_FLAG_COMPOSITE_DISPLAY
20 #define SKIP PIC_FLAG_SKIP
21 #define TAGS PIC_FLAG_TAGS
22 #define REPEAT_FIRST PIC_FLAG_REPEAT_FIRST_FIELD
23 #define COMPOSITE_MASK PIC_MASK_COMPOSITE_DISPLAY
30 static int cadence[12];
33 /**********************************************************************
35 *********************************************************************/
36 typedef struct hb_libmpeg2_s
38 mpeg2dec_t * libmpeg2;
39 const mpeg2_info_t * info;
45 int got_iframe; /* set when we get our first iframe */
46 int look_for_iframe; /* need an iframe to add chap break */
47 int look_for_break; /* need gop start to add chap break */
48 uint32_t nframes; /* number of frames we've decoded */
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 hb_buffer_t *hb_copy_frame( hb_job_t *job, int width, int height,
69 uint8_t* y, uint8_t *u, uint8_t *v )
71 int dst_w = width, dst_h = height;
74 dst_w = job->title->width;
75 dst_h = job->title->height;
77 int dst_wh = dst_w * dst_h;
78 hb_buffer_t *buf = hb_video_buffer_init( dst_w, dst_h );
80 if ( dst_w != width || dst_h != height )
82 // we're encoding and the frame dimensions don't match the title dimensions -
83 // rescale & matte Y, U, V into our output buf.
85 avpicture_alloc(&in, PIX_FMT_YUV420P, width, height );
86 avpicture_alloc(&out, PIX_FMT_YUV420P, dst_w, dst_h );
88 int src_wh = width * height;
89 memcpy( in.data[0], y, src_wh );
90 memcpy( in.data[1], u, src_wh >> 2 );
91 memcpy( in.data[2], v, src_wh >> 2 );
92 struct SwsContext *context = sws_getContext( width, height, PIX_FMT_YUV420P,
93 dst_w, dst_h, PIX_FMT_YUV420P,
94 SWS_LANCZOS|SWS_ACCURATE_RND,
96 sws_scale( context, in.data, in.linesize, 0, height, out.data, out.linesize );
97 sws_freeContext( context );
99 u_int8_t *data = buf->data;
100 memcpy( data, out.data[0], dst_wh );
102 // U & V planes are 1/4 the size of Y plane.
104 memcpy( data, out.data[1], dst_wh );
106 memcpy( data, out.data[2], dst_wh );
108 avpicture_free( &out );
109 avpicture_free( &in );
113 // we're scanning or the frame dimensions match the title's dimensions
114 // so we can do a straight copy.
115 u_int8_t *data = buf->data;
116 memcpy( data, y, dst_wh );
118 // U & V planes are 1/4 the size of Y plane.
120 memcpy( data, u, dst_wh );
122 memcpy( data, v, dst_wh );
127 /**********************************************************************
129 **********************************************************************
131 *********************************************************************/
132 static int hb_libmpeg2_decode( hb_libmpeg2_t * m, hb_buffer_t * buf_es,
133 hb_list_t * list_raw )
141 if( buf_es->start > -1 )
143 mpeg2_tag_picture( m->libmpeg2, buf_es->start >> 32,
144 buf_es->start & 0xFFFFFFFF );
146 mpeg2_buffer( m->libmpeg2, buf_es->data,
147 buf_es->data + buf_es->size );
152 state = mpeg2_parse( m->libmpeg2 );
153 if( state == STATE_BUFFER )
155 /* Require some more data */
158 else if( state == STATE_SEQUENCE )
160 if( !( m->width && m->height && m->rate ) )
162 m->width = m->info->sequence->width;
163 m->height = m->info->sequence->height;
164 m->rate = m->info->sequence->frame_period;
165 if ( m->aspect_ratio <= 0 && m->height &&
166 m->info->sequence->pixel_height )
168 /* mpeg2_parse doesn't store the aspect ratio. Instead
169 * it keeps the pixel width & height that would cause
170 * the storage width & height to come out in the correct
171 * aspect ratio. Convert these back to aspect ratio.
173 double ar_numer = m->width * m->info->sequence->pixel_width;
174 double ar_denom = m->height * m->info->sequence->pixel_height;
175 m->aspect_ratio = ar_numer / ar_denom;
179 else if( state == STATE_GOP && m->look_for_break)
181 // we were looking for a gop to add a chapter break - we found it
182 // so now start looking for an iframe.
183 m->look_for_iframe = m->look_for_break;
184 m->look_for_break = 0;
186 else if( ( state == STATE_SLICE || state == STATE_END ) &&
187 m->info->display_fbuf )
189 if( ( m->info->display_picture->flags &
190 PIC_MASK_CODING_TYPE ) == PIC_FLAG_CODING_TYPE_I )
192 // we got an iframe so we can start decoding video now
198 buf = hb_copy_frame( m->job, m->info->sequence->width,
199 m->info->sequence->height,
200 m->info->display_fbuf->buf[0],
201 m->info->display_fbuf->buf[1],
202 m->info->display_fbuf->buf[2] );
203 buf->sequence = buf_es->sequence;
205 if( m->info->display_picture->flags & PIC_FLAG_TAGS )
208 ( (uint64_t) m->info->display_picture->tag << 32 ) |
209 ( (uint64_t) m->info->display_picture->tag2 );
211 else if( m->last_pts > -1 )
213 /* For some reason nb_fields is sometimes 1 while it
215 buf->start = m->last_pts +
216 MAX( 2, m->info->display_picture->nb_fields ) *
217 m->info->sequence->frame_period / 600;
223 m->last_pts = buf->start;
225 if( m->look_for_iframe && ( m->info->display_picture->flags &
226 PIC_MASK_CODING_TYPE ) == PIC_FLAG_CODING_TYPE_I )
228 // we were waiting for an iframe to insert a chapter mark
230 buf->new_chap = m->look_for_iframe;
231 m->look_for_iframe = 0;
232 const char *chap_name = "";
233 if ( m->job && buf->new_chap > 0 &&
234 hb_list_item( m->job->title->list_chapter,
235 buf->new_chap - 1 ) )
237 hb_chapter_t * c = hb_list_item( m->job->title->list_chapter,
239 chap_name = c->title;
241 hb_log( "mpeg2: \"%s\" (%d) at frame %u time %lld",
242 chap_name, buf->new_chap, m->nframes, buf->start );
243 } else if ( m->nframes == 0 && m->job &&
244 hb_list_item( m->job->title->list_chapter,
245 m->job->chapter_start - 1 ) )
247 hb_chapter_t * c = hb_list_item( m->job->title->list_chapter,
248 m->job->chapter_start - 1 );
249 hb_log( "mpeg2: \"%s\" (%d) at frame %u time %lld", c->title,
250 m->job->chapter_start, m->nframes, buf->start );
254 flag = m->info->display_picture->flags;
256 /* Uncomment this block to see frame-by-frame picture flags, as the video encodes.
257 hb_log("***** MPEG 2 Picture Info for PTS %lld *****", buf->start);
258 if( flag & TOP_FIRST )
259 hb_log("MPEG2 Flag: Top field first");
260 if( flag & PROGRESSIVE )
261 hb_log("MPEG2 Flag: Progressive");
262 if( flag & COMPOSITE )
263 hb_log("MPEG2 Flag: Composite");
265 hb_log("MPEG2 Flag: Skip!");
267 hb_log("MPEG2 Flag: TAGS");
268 if(flag & REPEAT_FIRST )
269 hb_log("MPEG2 Flag: Repeat first field");
270 if( flag & COMPOSITE_MASK )
271 hb_log("MPEG2 Flag: Composite mask");
272 hb_log("fields: %d", m->info->display_picture->nb_fields);
274 /* Rotate the cadence tracking. */
276 for(i=11; i > 0; i--)
278 cadence[i] = cadence[i-1];
281 if ( !(flag & PROGRESSIVE) && !(flag & TOP_FIRST) )
283 /* Not progressive, not top first...
284 That means it's probably bottom
285 first, 2 fields displayed.
287 //hb_log("MPEG2 Flag: Bottom field first, 2 fields displayed.");
290 else if ( !(flag & PROGRESSIVE) && (flag & TOP_FIRST) )
292 /* Not progressive, top is first,
293 Two fields displayed.
295 //hb_log("MPEG2 Flag: Top field first, 2 fields displayed.");
298 else if ( (flag & PROGRESSIVE) && !(flag & TOP_FIRST) && !( flag & REPEAT_FIRST ) )
300 /* Progressive, but noting else.
301 That means Bottom first,
304 //hb_log("MPEG2 Flag: Progressive. Bottom field first, 2 fields displayed.");
305 cadence[0] = BT_PROG;
307 else if ( (flag & PROGRESSIVE) && !(flag & TOP_FIRST) && ( flag & REPEAT_FIRST ) )
309 /* Progressive, and repeat. .
310 That means Bottom first,
313 //hb_log("MPEG2 Flag: Progressive repeat. Bottom field first, 3 fields displayed.");
314 cadence[0] = BTB_PROG;
316 else if ( (flag & PROGRESSIVE) && (flag & TOP_FIRST) && !( flag & REPEAT_FIRST ) )
318 /* Progressive, top first.
319 That means top first,
322 //hb_log("MPEG2 Flag: Progressive. Top field first, 2 fields displayed.");
323 cadence[0] = TB_PROG;
325 else if ( (flag & PROGRESSIVE) && (flag & TOP_FIRST) && ( flag & REPEAT_FIRST ) )
327 /* Progressive, top, repeat.
328 That means top first,
331 //hb_log("MPEG2 Flag: Progressive repeat. Top field first, 3 fields displayed.");
332 cadence[0] = TBT_PROG;
335 if ( (cadence[2] <= TB) && (cadence[1] <= TB) && (cadence[0] > TB) && (cadence[11]) )
336 hb_log("%fs: Video -> Film", (float)buf->start / 90000);
337 if ( (cadence[2] > TB) && (cadence[1] <= TB) && (cadence[0] <= TB) && (cadence[11]) )
338 hb_log("%fs: Film -> Video", (float)buf->start / 90000);
340 /* Store picture flags for later use by filters */
341 buf->flags = m->info->display_picture->flags;
343 hb_list_add( list_raw, buf );
346 else if( state == STATE_INVALID )
348 mpeg2_reset( m->libmpeg2, 0 );
354 /**********************************************************************
356 **********************************************************************
358 *********************************************************************/
359 static void hb_libmpeg2_close( hb_libmpeg2_t ** _m )
361 hb_libmpeg2_t * m = *_m;
363 mpeg2_close( m->libmpeg2 );
369 /**********************************************************************
370 * The decmpeg2 work object
371 **********************************************************************
373 *********************************************************************/
374 struct hb_work_private_s
376 hb_libmpeg2_t * libmpeg2;
380 /**********************************************************************
381 * hb_work_decmpeg2_init
382 **********************************************************************
384 *********************************************************************/
385 static int decmpeg2Init( hb_work_object_t * w, hb_job_t * job )
387 hb_work_private_t * pv;
389 pv = calloc( 1, sizeof( hb_work_private_t ) );
390 w->private_data = pv;
392 pv->libmpeg2 = hb_libmpeg2_init();
393 pv->list = hb_list_init();
395 pv->libmpeg2->job = job;
400 /**********************************************************************
402 **********************************************************************
404 *********************************************************************/
405 static int decmpeg2Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
406 hb_buffer_t ** buf_out )
408 hb_work_private_t * pv = w->private_data;
409 hb_buffer_t * buf, * last = NULL;
410 int status = HB_WORK_OK;
412 // The reader found a chapter break, consume it completely, and remove it from the
413 // stream. We need to shift it.
414 if( (*buf_in)->new_chap )
416 pv->libmpeg2->look_for_break = (*buf_in)->new_chap;
417 (*buf_in)->new_chap = 0;
420 hb_libmpeg2_decode( pv->libmpeg2, *buf_in, pv->list );
422 /* if we got an empty buffer signaling end-of-stream send it downstream */
423 if ( (*buf_in)->size == 0 )
425 hb_list_add( pv->list, *buf_in );
427 status = HB_WORK_DONE;
431 while( ( buf = hb_list_item( pv->list, 0 ) ) )
433 hb_list_rem( pv->list, buf );
449 /**********************************************************************
451 **********************************************************************
453 *********************************************************************/
454 static void decmpeg2Close( hb_work_object_t * w )
456 hb_work_private_t * pv = w->private_data;
458 // don't log during scan
459 if ( pv->libmpeg2->job )
461 hb_log( "mpeg2 done: %d frames", pv->libmpeg2->nframes );
463 hb_list_close( &pv->list );
464 hb_libmpeg2_close( &pv->libmpeg2 );
468 static int decmpeg2Info( hb_work_object_t *w, hb_work_info_t *info )
470 hb_work_private_t *pv = w->private_data;
472 memset( info, 0, sizeof(*info) );
474 if ( pv && pv->libmpeg2 && pv->libmpeg2->info && pv->libmpeg2->info->sequence )
476 hb_libmpeg2_t *m = pv->libmpeg2;
478 info->width = m->width;
479 info->height = m->height;
480 info->pixel_aspect_width = m->info->sequence->pixel_width;
481 info->pixel_aspect_height = m->info->sequence->pixel_height;
482 info->aspect = m->aspect_ratio;
484 // if the frame is progressive & NTSC DVD height report it as 23.976 FPS
485 // so that scan can autodetect NTSC film
486 info->rate = 27000000;
487 info->rate_base = ( m->info->display_fbuf && m->info->display_picture &&
488 (m->info->display_picture->flags & PROGRESSIVE) &&
489 (m->height == 480 ) ) ? 1126125 : m->rate;
491 info->bitrate = m->info->sequence->byte_rate * 8;
492 info->profile = m->info->sequence->profile_level_id >> 4;
493 info->level = m->info->sequence->profile_level_id & 0xf;
494 info->name = "mpeg2";
500 hb_work_object_t hb_decmpeg2 =
503 "MPEG-2 decoder (libmpeg2)",