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;
41 int got_iframe; /* set when we get our first iframe */
42 int look_for_iframe; /* need an iframe to add chap break */
43 int look_for_break; /* need gop start to add chap break */
44 uint32_t nframes; /* number of frames we've decoded */
50 /**********************************************************************
52 **********************************************************************
54 *********************************************************************/
55 static hb_libmpeg2_t * hb_libmpeg2_init()
57 hb_libmpeg2_t * m = calloc( sizeof( hb_libmpeg2_t ), 1 );
59 m->libmpeg2 = mpeg2_init();
60 m->info = mpeg2_info( m->libmpeg2 );
66 static hb_buffer_t *hb_copy_frame( hb_job_t *job, int width, int height,
67 uint8_t* y, uint8_t *u, uint8_t *v )
69 int dst_w = width, dst_h = height;
72 dst_w = job->title->width;
73 dst_h = job->title->height;
75 int dst_wh = dst_w * dst_h;
76 hb_buffer_t *buf = hb_video_buffer_init( dst_w, dst_h );
78 if ( dst_w != width || dst_h != height )
80 // we're encoding and the frame dimensions don't match the title dimensions -
81 // rescale & matte Y, U, V into our output buf.
83 avpicture_alloc(&in, PIX_FMT_YUV420P, width, height );
84 avpicture_alloc(&out, PIX_FMT_YUV420P, dst_w, dst_h );
86 int src_wh = width * height;
87 memcpy( in.data[0], y, src_wh );
88 memcpy( in.data[1], u, src_wh >> 2 );
89 memcpy( in.data[2], v, src_wh >> 2 );
90 struct SwsContext *context = sws_getContext( width, height, PIX_FMT_YUV420P,
91 dst_w, dst_h, PIX_FMT_YUV420P,
92 SWS_LANCZOS|SWS_ACCURATE_RND,
94 sws_scale( context, in.data, in.linesize, 0, height, out.data, out.linesize );
95 sws_freeContext( context );
97 uint8_t *data = buf->data;
98 memcpy( data, out.data[0], dst_wh );
100 // U & V planes are 1/4 the size of Y plane.
102 memcpy( data, out.data[1], dst_wh );
104 memcpy( data, out.data[2], dst_wh );
106 avpicture_free( &out );
107 avpicture_free( &in );
111 // we're scanning or the frame dimensions match the title's dimensions
112 // so we can do a straight copy.
113 uint8_t *data = buf->data;
114 memcpy( data, y, dst_wh );
116 // U & V planes are 1/4 the size of Y plane.
118 memcpy( data, u, dst_wh );
120 memcpy( data, v, dst_wh );
125 /**********************************************************************
127 **********************************************************************
129 *********************************************************************/
130 static int hb_libmpeg2_decode( hb_libmpeg2_t * m, hb_buffer_t * buf_es,
131 hb_list_t * list_raw )
139 if( buf_es->start > -1 )
141 mpeg2_tag_picture( m->libmpeg2, buf_es->start >> 32,
142 buf_es->start & 0xFFFFFFFF );
144 mpeg2_buffer( m->libmpeg2, buf_es->data,
145 buf_es->data + buf_es->size );
150 state = mpeg2_parse( m->libmpeg2 );
151 if( state == STATE_BUFFER )
153 /* Require some more data */
156 else if( state == STATE_SEQUENCE )
158 if( !( m->width && m->height && m->rate ) )
160 m->width = m->info->sequence->width;
161 m->height = m->info->sequence->height;
162 m->rate = m->info->sequence->frame_period;
163 if ( m->aspect_ratio <= 0 && m->height &&
164 m->info->sequence->pixel_height )
166 /* mpeg2_parse doesn't store the aspect ratio. Instead
167 * it keeps the pixel width & height that would cause
168 * the storage width & height to come out in the correct
169 * aspect ratio. Convert these back to aspect ratio.
171 double ar_numer = m->width * m->info->sequence->pixel_width;
172 double ar_denom = m->height * m->info->sequence->pixel_height;
173 m->aspect_ratio = ar_numer / ar_denom;
177 else if( state == STATE_GOP && m->look_for_break)
179 // we were looking for a gop to add a chapter break - we found it
180 // so now start looking for an iframe.
181 m->look_for_iframe = m->look_for_break;
182 m->look_for_break = 0;
184 else if( ( state == STATE_SLICE || state == STATE_END ) &&
185 m->info->display_fbuf )
187 if( ( m->info->display_picture->flags &
188 PIC_MASK_CODING_TYPE ) == PIC_FLAG_CODING_TYPE_I )
190 // we got an iframe so we can start decoding video now
196 buf = hb_copy_frame( m->job, m->info->sequence->width,
197 m->info->sequence->height,
198 m->info->display_fbuf->buf[0],
199 m->info->display_fbuf->buf[1],
200 m->info->display_fbuf->buf[2] );
201 buf->sequence = buf_es->sequence;
203 if( m->info->display_picture->flags & PIC_FLAG_TAGS )
206 ( (uint64_t) m->info->display_picture->tag << 32 ) |
207 ( (uint64_t) m->info->display_picture->tag2 );
209 else if( m->last_pts > -1 )
211 /* For some reason nb_fields is sometimes 1 while it
213 buf->start = m->last_pts +
214 MAX( 2, m->info->display_picture->nb_fields ) *
215 m->info->sequence->frame_period / 600;
221 m->last_pts = buf->start;
223 if( m->look_for_iframe && ( m->info->display_picture->flags &
224 PIC_MASK_CODING_TYPE ) == PIC_FLAG_CODING_TYPE_I )
226 // we were waiting for an iframe to insert a chapter mark
228 buf->new_chap = m->look_for_iframe;
229 m->look_for_iframe = 0;
230 const char *chap_name = "";
231 if ( m->job && buf->new_chap > 0 &&
232 hb_list_item( m->job->title->list_chapter,
233 buf->new_chap - 1 ) )
235 hb_chapter_t * c = hb_list_item( m->job->title->list_chapter,
237 chap_name = c->title;
239 hb_log( "mpeg2: \"%s\" (%d) at frame %u time %lld",
240 chap_name, buf->new_chap, m->nframes, buf->start );
241 } else if ( m->nframes == 0 && m->job &&
242 hb_list_item( m->job->title->list_chapter,
243 m->job->chapter_start - 1 ) )
245 hb_chapter_t * c = hb_list_item( m->job->title->list_chapter,
246 m->job->chapter_start - 1 );
247 hb_log( "mpeg2: \"%s\" (%d) at frame %u time %lld", c->title,
248 m->job->chapter_start, m->nframes, buf->start );
252 m->flag = m->info->display_picture->flags;
254 /* Uncomment this block to see frame-by-frame picture flags, as the video encodes.
255 hb_log("***** MPEG 2 Picture Info for PTS %lld *****", buf->start);
256 if( m->flag & TOP_FIRST )
257 hb_log("MPEG2 Flag: Top field first");
258 if( m->flag & PROGRESSIVE )
259 hb_log("MPEG2 Flag: Progressive");
260 if( m->flag & COMPOSITE )
261 hb_log("MPEG2 Flag: Composite");
263 hb_log("MPEG2 Flag: Skip!");
265 hb_log("MPEG2 Flag: TAGS");
266 if(fm->lag & REPEAT_FIRST )
267 hb_log("MPEG2 Flag: Repeat first field");
268 if( m->flag & COMPOSITE_MASK )
269 hb_log("MPEG2 Flag: Composite mask");
270 hb_log("fields: %d", m->info->display_picture->nb_fields);
272 /* Rotate the cadence tracking. */
274 for(i=11; i > 0; i--)
276 m->cadence[i] = m->cadence[i-1];
279 if ( !(m->flag & PROGRESSIVE) && !(m->flag & TOP_FIRST) )
281 /* Not progressive, not top first...
282 That means it's probably bottom
283 first, 2 fields displayed.
285 //hb_log("MPEG2 Flag: Bottom field first, 2 fields displayed.");
288 else if ( !(m->flag & PROGRESSIVE) && (m->flag & TOP_FIRST) )
290 /* Not progressive, top is first,
291 Two fields displayed.
293 //hb_log("MPEG2 Flag: Top field first, 2 fields displayed.");
296 else if ( (m->flag & PROGRESSIVE) && !(m->flag & TOP_FIRST) && !( m->flag & REPEAT_FIRST ) )
298 /* Progressive, but noting else.
299 That means Bottom first,
302 //hb_log("MPEG2 Flag: Progressive. Bottom field first, 2 fields displayed.");
303 m->cadence[0] = BT_PROG;
305 else if ( (m->flag & PROGRESSIVE) && !(m->flag & TOP_FIRST) && ( m->flag & REPEAT_FIRST ) )
307 /* Progressive, and repeat. .
308 That means Bottom first,
311 //hb_log("MPEG2 Flag: Progressive repeat. Bottom field first, 3 fields displayed.");
312 m->cadence[0] = BTB_PROG;
314 else if ( (m->flag & PROGRESSIVE) && (m->flag & TOP_FIRST) && !( m->flag & REPEAT_FIRST ) )
316 /* Progressive, top first.
317 That means top first,
320 //hb_log("MPEG2 Flag: Progressive. Top field first, 2 fields displayed.");
321 m->cadence[0] = TB_PROG;
323 else if ( (m->flag & PROGRESSIVE) && (m->flag & TOP_FIRST) && ( m->flag & REPEAT_FIRST ) )
325 /* Progressive, top, repeat.
326 That means top first,
329 //hb_log("MPEG2 Flag: Progressive repeat. Top field first, 3 fields displayed.");
330 m->cadence[0] = TBT_PROG;
333 if ( (m->cadence[2] <= TB) && (m->cadence[1] <= TB) && (m->cadence[0] > TB) && (m->cadence[11]) )
334 hb_log("%fs: Video -> Film", (float)buf->start / 90000);
335 if ( (m->cadence[2] > TB) && (m->cadence[1] <= TB) && (m->cadence[0] <= TB) && (m->cadence[11]) )
336 hb_log("%fs: Film -> Video", (float)buf->start / 90000);
338 /* Store picture flags for later use by filters */
339 buf->flags = m->info->display_picture->flags;
341 hb_list_add( list_raw, buf );
344 else if( state == STATE_INVALID )
346 mpeg2_reset( m->libmpeg2, 0 );
352 /**********************************************************************
354 **********************************************************************
356 *********************************************************************/
357 static void hb_libmpeg2_close( hb_libmpeg2_t ** _m )
359 hb_libmpeg2_t * m = *_m;
361 mpeg2_close( m->libmpeg2 );
367 /**********************************************************************
368 * The decmpeg2 work object
369 **********************************************************************
371 *********************************************************************/
372 struct hb_work_private_s
374 hb_libmpeg2_t * libmpeg2;
378 /**********************************************************************
379 * hb_work_decmpeg2_init
380 **********************************************************************
382 *********************************************************************/
383 static int decmpeg2Init( hb_work_object_t * w, hb_job_t * job )
385 hb_work_private_t * pv;
387 pv = calloc( 1, sizeof( hb_work_private_t ) );
388 w->private_data = pv;
390 pv->libmpeg2 = hb_libmpeg2_init();
391 pv->list = hb_list_init();
393 pv->libmpeg2->job = job;
398 /**********************************************************************
400 **********************************************************************
402 *********************************************************************/
403 static int decmpeg2Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
404 hb_buffer_t ** buf_out )
406 hb_work_private_t * pv = w->private_data;
407 hb_buffer_t * buf, * last = NULL;
408 int status = HB_WORK_OK;
410 // The reader found a chapter break, consume it completely, and remove it from the
411 // stream. We need to shift it.
412 if( (*buf_in)->new_chap )
414 pv->libmpeg2->look_for_break = (*buf_in)->new_chap;
415 (*buf_in)->new_chap = 0;
418 hb_libmpeg2_decode( pv->libmpeg2, *buf_in, pv->list );
420 /* if we got an empty buffer signaling end-of-stream send it downstream */
421 if ( (*buf_in)->size == 0 )
423 hb_list_add( pv->list, *buf_in );
425 status = HB_WORK_DONE;
429 while( ( buf = hb_list_item( pv->list, 0 ) ) )
431 hb_list_rem( pv->list, buf );
447 /**********************************************************************
449 **********************************************************************
451 *********************************************************************/
452 static void decmpeg2Close( hb_work_object_t * w )
454 hb_work_private_t * pv = w->private_data;
456 // don't log during scan
457 if ( pv->libmpeg2->job )
459 hb_log( "mpeg2 done: %d frames", pv->libmpeg2->nframes );
461 hb_list_close( &pv->list );
462 hb_libmpeg2_close( &pv->libmpeg2 );
466 static int decmpeg2Info( hb_work_object_t *w, hb_work_info_t *info )
468 hb_work_private_t *pv = w->private_data;
470 memset( info, 0, sizeof(*info) );
472 if ( pv && pv->libmpeg2 && pv->libmpeg2->info && pv->libmpeg2->info->sequence )
474 hb_libmpeg2_t *m = pv->libmpeg2;
476 info->width = m->width;
477 info->height = m->height;
478 info->pixel_aspect_width = m->info->sequence->pixel_width;
479 info->pixel_aspect_height = m->info->sequence->pixel_height;
480 info->aspect = m->aspect_ratio;
482 // if the frame is progressive & NTSC DVD height report it as 23.976 FPS
483 // so that scan can autodetect NTSC film
484 info->rate = 27000000;
485 info->rate_base = ( m->info->display_fbuf && m->info->display_picture &&
486 (m->info->display_picture->flags & PROGRESSIVE) &&
487 (m->height == 480 ) ) ? 1126125 : m->rate;
489 info->bitrate = m->info->sequence->byte_rate * 8;
490 info->profile = m->info->sequence->profile_level_id >> 4;
491 info->level = m->info->sequence->profile_level_id & 0xf;
492 info->name = "mpeg2";
498 hb_work_object_t hb_decmpeg2 =
501 "MPEG-2 decoder (libmpeg2)",