OSDN Git Service

Consolidate all the ffmpeg-related includes into libhb/hbffmpeg.h then prototype...
[handbrake-jp/handbrake-jp-git.git] / libhb / decmpeg2.c
1 /* $Id: decmpeg2.c,v 1.12 2005/03/03 16:30:42 titer Exp $
2
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. */
6
7 #include "hb.h"
8 #include "hbffmpeg.h"
9 #include "mpeg2dec/mpeg2.h"
10
11 /* Cadence tracking */
12 #ifndef PIC_FLAG_REPEAT_FIRST_FIELD
13 #define PIC_FLAG_REPEAT_FIRST_FIELD 256
14 #endif
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
22 #define TB 8
23 #define BT 16
24 #define BT_PROG 32
25 #define BTB_PROG 64
26 #define TB_PROG 128
27 #define TBT_PROG 256
28 static int cadence[12];
29 static int flag = 0;
30
31 /**********************************************************************
32  * hb_libmpeg2_t
33  *********************************************************************/
34 typedef struct hb_libmpeg2_s
35 {
36     mpeg2dec_t         * libmpeg2;
37     const mpeg2_info_t * info;
38     hb_job_t           * job;
39     int                  width;
40     int                  height;
41     int                  rate;
42     double               aspect_ratio;
43     int                  got_iframe;        /* set when we get our first iframe */
44     int                  look_for_iframe;   /* need an iframe to add chap break */
45     int                  look_for_break;    /* need gop start to add chap break */
46     uint32_t             nframes;           /* number of frames we've decoded */
47     int64_t              last_pts;
48 } hb_libmpeg2_t;
49
50 /**********************************************************************
51  * hb_libmpeg2_init
52  **********************************************************************
53  *
54  *********************************************************************/
55 static hb_libmpeg2_t * hb_libmpeg2_init()
56 {
57     hb_libmpeg2_t * m = calloc( sizeof( hb_libmpeg2_t ), 1 );
58
59     m->libmpeg2 = mpeg2_init();
60     m->info     = mpeg2_info( m->libmpeg2 );
61     m->last_pts = -1;
62
63     return m;
64 }
65
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 )
68 {
69     int dst_w = width, dst_h = height;
70     if ( job )
71     {
72         dst_w = job->title->width;
73         dst_h = job->title->height;
74     }
75     int dst_wh = dst_w * dst_h;
76     hb_buffer_t *buf  = hb_video_buffer_init( dst_w, dst_h );
77
78     if ( dst_w != width || dst_h != height )
79     {
80         // we're encoding and the frame dimensions don't match the title dimensions -
81         // rescale & matte Y, U, V into our output buf.
82         AVPicture in, out;
83         avpicture_alloc(&in,  PIX_FMT_YUV420P, width, height );
84         avpicture_alloc(&out, PIX_FMT_YUV420P, dst_w, dst_h );
85
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,
93                                                      NULL, NULL, NULL );
94         sws_scale( context, in.data, in.linesize, 0, height, out.data, out.linesize );
95         sws_freeContext( context );
96
97         u_int8_t *data = buf->data;
98         memcpy( data, out.data[0], dst_wh );
99         data += dst_wh;
100         // U & V planes are 1/4 the size of Y plane.
101         dst_wh >>= 2;
102         memcpy( data, out.data[1], dst_wh );
103         data += dst_wh;
104         memcpy( data, out.data[2], dst_wh );
105
106         avpicture_free( &out );
107         avpicture_free( &in );
108     }
109     else
110     {
111         // we're scanning or the frame dimensions match the title's dimensions
112         // so we can do a straight copy.
113         u_int8_t *data = buf->data;
114         memcpy( data, y, dst_wh );
115         data += dst_wh;
116         // U & V planes are 1/4 the size of Y plane.
117         dst_wh >>= 2;
118         memcpy( data, u, dst_wh );
119         data += dst_wh;
120         memcpy( data, v, dst_wh );
121     }
122     return buf;
123 }
124
125 /**********************************************************************
126  * hb_libmpeg2_decode
127  **********************************************************************
128  *
129  *********************************************************************/
130 static int hb_libmpeg2_decode( hb_libmpeg2_t * m, hb_buffer_t * buf_es,
131                         hb_list_t * list_raw )
132 {
133     mpeg2_state_t   state;
134     hb_buffer_t   * buf;
135
136     if ( buf_es->size )
137     {
138         /* Feed libmpeg2 */
139         if( buf_es->start > -1 )
140         {
141             mpeg2_tag_picture( m->libmpeg2, buf_es->start >> 32,
142                                buf_es->start & 0xFFFFFFFF );
143         }
144         mpeg2_buffer( m->libmpeg2, buf_es->data,
145                       buf_es->data + buf_es->size );
146     }
147
148     for( ;; )
149     {
150         state = mpeg2_parse( m->libmpeg2 );
151         if( state == STATE_BUFFER )
152         {
153             /* Require some more data */
154             break;
155         }
156         else if( state == STATE_SEQUENCE )
157         {
158             if( !( m->width && m->height && m->rate ) )
159             {
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 )
165                 {
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.
170                      */
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;
174                 }
175             }
176         }
177         else if( state == STATE_GOP && m->look_for_break)
178         {
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;
183         }
184         else if( ( state == STATE_SLICE || state == STATE_END ) &&
185                  m->info->display_fbuf )
186         {
187             if( ( m->info->display_picture->flags &
188                   PIC_MASK_CODING_TYPE ) == PIC_FLAG_CODING_TYPE_I )
189             {
190                 // we got an iframe so we can start decoding video now
191                 m->got_iframe = 1;
192             }
193
194             if( m->got_iframe )
195             {
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;
202
203                 if( m->info->display_picture->flags & PIC_FLAG_TAGS )
204                 {
205                     buf->start =
206                         ( (uint64_t) m->info->display_picture->tag << 32 ) |
207                         ( (uint64_t) m->info->display_picture->tag2 );
208                 }
209                 else if( m->last_pts > -1 )
210                 {
211                     /* For some reason nb_fields is sometimes 1 while it
212                        should be 2 */
213                     buf->start = m->last_pts +
214                         MAX( 2, m->info->display_picture->nb_fields ) *
215                         m->info->sequence->frame_period / 600;
216                 }
217                 else
218                 {
219                     buf->start = -1;
220                 }
221                 m->last_pts = buf->start;
222
223                 if( m->look_for_iframe && ( m->info->display_picture->flags &
224                       PIC_MASK_CODING_TYPE ) == PIC_FLAG_CODING_TYPE_I )
225                 {
226                     // we were waiting for an iframe to insert a chapter mark
227                     // and we have one.
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 ) )
234                     {
235                         hb_chapter_t * c = hb_list_item( m->job->title->list_chapter,
236                                                          buf->new_chap - 1 );
237                         chap_name = c->title;
238                     }
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 ) )
244                 {
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 );
249                 }
250                 ++m->nframes;
251
252                 flag = m->info->display_picture->flags;
253
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( flag & TOP_FIRST )
257                     hb_log("MPEG2 Flag: Top field first");
258                 if( flag & PROGRESSIVE )
259                     hb_log("MPEG2 Flag: Progressive");
260                 if( flag & COMPOSITE )
261                     hb_log("MPEG2 Flag: Composite");
262                 if( flag & SKIP )
263                     hb_log("MPEG2 Flag: Skip!");
264                 if( flag & TAGS )
265                     hb_log("MPEG2 Flag: TAGS");
266                 if(flag & REPEAT_FIRST )
267                     hb_log("MPEG2 Flag: Repeat first field");
268                 if( flag & COMPOSITE_MASK )
269                     hb_log("MPEG2 Flag: Composite mask");
270                 hb_log("fields: %d", m->info->display_picture->nb_fields);
271 */
272                 /*  Rotate the cadence tracking. */
273                 int i = 0;
274                 for(i=11; i > 0; i--)
275                 {
276                     cadence[i] = cadence[i-1];
277                 }
278
279                 if ( !(flag & PROGRESSIVE) && !(flag & TOP_FIRST) )
280                 {
281                     /* Not progressive, not top first...
282                        That means it's probably bottom
283                        first, 2 fields displayed.
284                     */
285                     //hb_log("MPEG2 Flag: Bottom field first, 2 fields displayed.");
286                     cadence[0] = BT;
287                 }
288                 else if ( !(flag & PROGRESSIVE) && (flag & TOP_FIRST) )
289                 {
290                     /* Not progressive, top is first,
291                        Two fields displayed.
292                     */
293                     //hb_log("MPEG2 Flag: Top field first, 2 fields displayed.");
294                     cadence[0] = TB;
295                 }
296                 else if ( (flag & PROGRESSIVE) && !(flag & TOP_FIRST) && !( flag & REPEAT_FIRST )  )
297                 {
298                     /* Progressive, but noting else.
299                        That means Bottom first,
300                        2 fields displayed.
301                     */
302                     //hb_log("MPEG2 Flag: Progressive. Bottom field first, 2 fields displayed.");
303                     cadence[0] = BT_PROG;
304                 }
305                 else if ( (flag & PROGRESSIVE) && !(flag & TOP_FIRST) && ( flag & REPEAT_FIRST )  )
306                 {
307                     /* Progressive, and repeat. .
308                        That means Bottom first,
309                        3 fields displayed.
310                     */
311                     //hb_log("MPEG2 Flag: Progressive repeat. Bottom field first, 3 fields displayed.");
312                     cadence[0] = BTB_PROG;
313                 }
314                 else if ( (flag & PROGRESSIVE) && (flag & TOP_FIRST) && !( flag & REPEAT_FIRST )  )
315                 {
316                     /* Progressive, top first.
317                        That means top first,
318                        2 fields displayed.
319                     */
320                     //hb_log("MPEG2 Flag: Progressive. Top field first, 2 fields displayed.");
321                     cadence[0] = TB_PROG;
322                 }
323                 else if ( (flag & PROGRESSIVE) && (flag & TOP_FIRST) && ( flag & REPEAT_FIRST )  )
324                 {
325                     /* Progressive, top, repeat.
326                        That means top first,
327                        3 fields displayed.
328                     */
329                     //hb_log("MPEG2 Flag: Progressive repeat. Top field first, 3 fields displayed.");
330                     cadence[0] = TBT_PROG;
331                 }
332
333                 if ( (cadence[2] <= TB) && (cadence[1] <= TB) && (cadence[0] > TB) && (cadence[11]) )
334                     hb_log("%fs: Video -> Film", (float)buf->start / 90000);
335                 if ( (cadence[2] > TB) && (cadence[1] <= TB) && (cadence[0] <= TB) && (cadence[11]) )
336                     hb_log("%fs: Film -> Video", (float)buf->start / 90000);
337
338                 /* Store picture flags for later use by filters */
339                 buf->flags = m->info->display_picture->flags;
340
341                 hb_list_add( list_raw, buf );
342             }
343         }
344         else if( state == STATE_INVALID )
345         {
346             mpeg2_reset( m->libmpeg2, 0 );
347         }
348     }
349     return 1;
350 }
351
352 /**********************************************************************
353  * hb_libmpeg2_close
354  **********************************************************************
355  *
356  *********************************************************************/
357 static void hb_libmpeg2_close( hb_libmpeg2_t ** _m )
358 {
359     hb_libmpeg2_t * m = *_m;
360
361     mpeg2_close( m->libmpeg2 );
362
363     free( m );
364     *_m = NULL;
365 }
366
367 /**********************************************************************
368  * The decmpeg2 work object
369  **********************************************************************
370  *
371  *********************************************************************/
372 struct hb_work_private_s
373 {
374     hb_libmpeg2_t * libmpeg2;
375     hb_list_t     * list;
376 };
377
378 /**********************************************************************
379  * hb_work_decmpeg2_init
380  **********************************************************************
381  *
382  *********************************************************************/
383 static int decmpeg2Init( hb_work_object_t * w, hb_job_t * job )
384 {
385     hb_work_private_t * pv;
386
387     pv              = calloc( 1, sizeof( hb_work_private_t ) );
388     w->private_data = pv;
389
390     pv->libmpeg2 = hb_libmpeg2_init();
391     pv->list     = hb_list_init();
392
393     pv->libmpeg2->job = job;
394
395     return 0;
396 }
397
398 /**********************************************************************
399  * Work
400  **********************************************************************
401  *
402  *********************************************************************/
403 static int decmpeg2Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
404                    hb_buffer_t ** buf_out )
405 {
406     hb_work_private_t * pv = w->private_data;
407     hb_buffer_t * buf, * last = NULL;
408     int status = HB_WORK_OK;
409
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 )
413     {
414         pv->libmpeg2->look_for_break = (*buf_in)->new_chap;
415         (*buf_in)->new_chap = 0;
416     }
417
418     hb_libmpeg2_decode( pv->libmpeg2, *buf_in, pv->list );
419
420     /* if we got an empty buffer signaling end-of-stream send it downstream */
421     if ( (*buf_in)->size == 0 )
422     {
423         hb_list_add( pv->list, *buf_in );
424         *buf_in = NULL;
425         status = HB_WORK_DONE;
426     }
427
428     *buf_out = NULL;
429     while( ( buf = hb_list_item( pv->list, 0 ) ) )
430     {
431         hb_list_rem( pv->list, buf );
432         if( last )
433         {
434             last->next = buf;
435             last       = buf;
436         }
437         else
438         {
439             *buf_out = buf;
440             last     = buf;
441         }
442     }
443
444     return status;
445 }
446
447 /**********************************************************************
448  * Close
449  **********************************************************************
450  *
451  *********************************************************************/
452 static void decmpeg2Close( hb_work_object_t * w )
453 {
454     hb_work_private_t * pv = w->private_data;
455
456     // don't log during scan
457     if ( pv->libmpeg2->job )
458     {
459         hb_log( "mpeg2 done: %d frames", pv->libmpeg2->nframes );
460     }
461     hb_list_close( &pv->list );
462     hb_libmpeg2_close( &pv->libmpeg2 );
463     free( pv );
464 }
465
466 static int decmpeg2Info( hb_work_object_t *w, hb_work_info_t *info )
467 {
468     hb_work_private_t *pv = w->private_data;
469
470     memset( info, 0, sizeof(*info) );
471
472     if ( pv && pv->libmpeg2 && pv->libmpeg2->info && pv->libmpeg2->info->sequence )
473     {
474         hb_libmpeg2_t *m = pv->libmpeg2;
475
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;
481
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;
488
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";
493         return 1;
494     }
495     return 0;
496 }
497
498 hb_work_object_t hb_decmpeg2 =
499 {
500     WORK_DECMPEG2,
501     "MPEG-2 decoder (libmpeg2)",
502     decmpeg2Init,
503     decmpeg2Work,
504     decmpeg2Close,
505     decmpeg2Info
506 };
507