OSDN Git Service

CLI: Missed file from SubRip - a symptom of too many views and patches
[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
29 /**********************************************************************
30  * hb_libmpeg2_t
31  *********************************************************************/
32 typedef struct hb_libmpeg2_s
33 {
34     mpeg2dec_t         * libmpeg2;
35     const mpeg2_info_t * info;
36     hb_job_t           * job;
37     hb_title_t         * title;
38     int                  width;
39     int                  height;
40     int                  rate;
41     double               aspect_ratio;
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 */
46     int64_t              last_pts;
47     int cadence[12];
48     int flag;
49     hb_list_t          * list_subtitle;
50 } hb_libmpeg2_t;
51
52 /**********************************************************************
53  * hb_libmpeg2_init
54  **********************************************************************
55  *
56  *********************************************************************/
57 static hb_libmpeg2_t * hb_libmpeg2_init()
58 {
59     hb_libmpeg2_t * m = calloc( sizeof( hb_libmpeg2_t ), 1 );
60
61     m->libmpeg2 = mpeg2_init();
62     m->info     = mpeg2_info( m->libmpeg2 );
63     m->last_pts = -1;
64
65     return m;
66 }
67
68 static void hb_mpeg2_cc( hb_libmpeg2_t * m, uint8_t *cc_block )
69 {
70     uint8_t cc_valid = (*cc_block & 4) >>2;
71     uint8_t cc_type = *cc_block & 3;
72     hb_buffer_t *cc_buf;
73     int i;
74     hb_subtitle_t * subtitle;
75     
76     if( !m->job )
77     {
78         /*
79          * Ignore CC decoding during scanning.
80          */
81         return;
82     }
83
84     if (cc_valid || cc_type==3)
85     {
86         switch (cc_type)
87         {
88         case 0:
89             // CC1 stream
90             for (i = 0; i < hb_list_count( m->list_subtitle ); i++)
91             {
92                 subtitle = hb_list_item( m->list_subtitle, i );
93                 cc_buf = hb_buffer_init( 2 );
94                 if( cc_buf )
95                 {
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 );
99                 }
100             }
101             break;
102         case 1:
103             // CC2 stream
104             //process608( cc_block+1, 2, &m->cc608 );
105             break;
106         case 2: //EIA-708
107             // DTVCC packet data
108             // Fall through
109         case 3: //EIA-708
110         {
111             uint8_t temp[4];
112             temp[0]=cc_valid;
113             temp[1]=cc_type;
114             temp[2]=cc_block[1];
115             temp[3]=cc_block[2];
116             //do_708 ((const unsigned char *) temp, 4);
117         }
118         break;
119         default:
120             break;
121         } 
122     } 
123     else
124     {
125         hb_log("Ignoring invalid CC block");
126     }
127 }
128
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 )
131 {
132     int dst_w = width, dst_h = height;
133     if ( job )
134     {
135         dst_w = job->title->width;
136         dst_h = job->title->height;
137     }
138     int dst_wh = dst_w * dst_h;
139     hb_buffer_t *buf  = hb_video_buffer_init( dst_w, dst_h );
140
141     if ( dst_w != width || dst_h != height )
142     {
143         // we're encoding and the frame dimensions don't match the title dimensions -
144         // rescale & matte Y, U, V into our output buf.
145         AVPicture in, out;
146         avpicture_alloc(&in,  PIX_FMT_YUV420P, width, height );
147         avpicture_alloc(&out, PIX_FMT_YUV420P, dst_w, dst_h );
148
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,
156                                                      NULL, NULL, NULL );
157         sws_scale( context, in.data, in.linesize, 0, height, out.data, out.linesize );
158         sws_freeContext( context );
159
160         uint8_t *data = buf->data;
161         memcpy( data, out.data[0], dst_wh );
162         data += dst_wh;
163         // U & V planes are 1/4 the size of Y plane.
164         dst_wh >>= 2;
165         memcpy( data, out.data[1], dst_wh );
166         data += dst_wh;
167         memcpy( data, out.data[2], dst_wh );
168
169         avpicture_free( &out );
170         avpicture_free( &in );
171     }
172     else
173     {
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 );
178         data += dst_wh;
179         // U & V planes are 1/4 the size of Y plane.
180         dst_wh >>= 2;
181         memcpy( data, u, dst_wh );
182         data += dst_wh;
183         memcpy( data, v, dst_wh );
184     }
185     return buf;
186 }
187
188 /**********************************************************************
189  * hb_libmpeg2_decode
190  **********************************************************************
191  *
192  *********************************************************************/
193 static int hb_libmpeg2_decode( hb_libmpeg2_t * m, hb_buffer_t * buf_es,
194                                hb_list_t * list_raw )
195 {
196     mpeg2_state_t   state;
197     hb_buffer_t   * buf;
198
199     if ( buf_es->size )
200     {
201         /* Feed libmpeg2 */
202         if( buf_es->start > -1 )
203         {
204             mpeg2_tag_picture( m->libmpeg2, buf_es->start >> 32,
205                                buf_es->start & 0xFFFFFFFF );
206         }
207         mpeg2_buffer( m->libmpeg2, buf_es->data,
208                       buf_es->data + buf_es->size );
209     }
210
211     for( ;; )
212     {
213         state = mpeg2_parse( m->libmpeg2 );
214         if( state == STATE_BUFFER )
215         {
216             /* Require some more data */
217             break;
218         }
219         else if( state == STATE_SEQUENCE )
220         {
221             if( !( m->width && m->height && m->rate ) )
222             {
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 )
228                 {
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.
233                      */
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;
237                 }
238             }
239         }
240         else if( state == STATE_GOP && m->look_for_break)
241         {
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;
246         }
247         else if( ( state == STATE_SLICE || state == STATE_END ) &&
248                  m->info->display_fbuf )
249         {
250             if( ( m->info->display_picture->flags &
251                   PIC_MASK_CODING_TYPE ) == PIC_FLAG_CODING_TYPE_I )
252             {
253                 // we got an iframe so we can start decoding video now
254                 m->got_iframe = 1;
255             }
256
257             if( m->got_iframe )
258             {
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;
265
266                 if( m->info->display_picture->flags & PIC_FLAG_TAGS )
267                 {
268                     buf->start =
269                         ( (uint64_t) m->info->display_picture->tag << 32 ) |
270                         ( (uint64_t) m->info->display_picture->tag2 );
271                 }
272                 else if( m->last_pts > -1 )
273                 {
274                     /* For some reason nb_fields is sometimes 1 while it
275                        should be 2 */
276                     buf->start = m->last_pts +
277                         MAX( 2, m->info->display_picture->nb_fields ) *
278                         m->info->sequence->frame_period / 600;
279                 }
280                 else
281                 {
282                     buf->start = -1;
283                 }
284                 m->last_pts = buf->start;
285
286                 if( m->look_for_iframe && ( m->info->display_picture->flags &
287                       PIC_MASK_CODING_TYPE ) == PIC_FLAG_CODING_TYPE_I )
288                 {
289                     // we were waiting for an iframe to insert a chapter mark
290                     // and we have one.
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 ) )
297                     {
298                         hb_chapter_t * c = hb_list_item( m->job->title->list_chapter,
299                                                          buf->new_chap - 1 );
300                         chap_name = c->title;
301                     }
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 ) )
307                 {
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 );
312                 }
313                 ++m->nframes;
314
315                 m->flag = m->info->display_picture->flags;
316
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");
325                 if( m->flag & SKIP )
326                     hb_log("MPEG2 Flag: Skip!");
327                 if( m->flag & TAGS )
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);
334 */
335                 /*  Rotate the cadence tracking. */
336                 int i = 0;
337                 for(i=11; i > 0; i--)
338                 {
339                     m->cadence[i] = m->cadence[i-1];
340                 }
341
342                 if ( !(m->flag & PROGRESSIVE) && !(m->flag & TOP_FIRST) )
343                 {
344                     /* Not progressive, not top first...
345                        That means it's probably bottom
346                        first, 2 fields displayed.
347                     */
348                     //hb_log("MPEG2 Flag: Bottom field first, 2 fields displayed.");
349                     m->cadence[0] = BT;
350                 }
351                 else if ( !(m->flag & PROGRESSIVE) && (m->flag & TOP_FIRST) )
352                 {
353                     /* Not progressive, top is first,
354                        Two fields displayed.
355                     */
356                     //hb_log("MPEG2 Flag: Top field first, 2 fields displayed.");
357                     m->cadence[0] = TB;
358                 }
359                 else if ( (m->flag & PROGRESSIVE) && !(m->flag & TOP_FIRST) && !( m->flag & REPEAT_FIRST )  )
360                 {
361                     /* Progressive, but noting else.
362                        That means Bottom first,
363                        2 fields displayed.
364                     */
365                     //hb_log("MPEG2 Flag: Progressive. Bottom field first, 2 fields displayed.");
366                     m->cadence[0] = BT_PROG;
367                 }
368                 else if ( (m->flag & PROGRESSIVE) && !(m->flag & TOP_FIRST) && ( m->flag & REPEAT_FIRST )  )
369                 {
370                     /* Progressive, and repeat. .
371                        That means Bottom first,
372                        3 fields displayed.
373                     */
374                     //hb_log("MPEG2 Flag: Progressive repeat. Bottom field first, 3 fields displayed.");
375                     m->cadence[0] = BTB_PROG;
376                 }
377                 else if ( (m->flag & PROGRESSIVE) && (m->flag & TOP_FIRST) && !( m->flag & REPEAT_FIRST )  )
378                 {
379                     /* Progressive, top first.
380                        That means top first,
381                        2 fields displayed.
382                     */
383                     //hb_log("MPEG2 Flag: Progressive. Top field first, 2 fields displayed.");
384                     m->cadence[0] = TB_PROG;
385                 }
386                 else if ( (m->flag & PROGRESSIVE) && (m->flag & TOP_FIRST) && ( m->flag & REPEAT_FIRST )  )
387                 {
388                     /* Progressive, top, repeat.
389                        That means top first,
390                        3 fields displayed.
391                     */
392                     //hb_log("MPEG2 Flag: Progressive repeat. Top field first, 3 fields displayed.");
393                     m->cadence[0] = TBT_PROG;
394                 }
395
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);
400
401                 /* Store picture flags for later use by filters */
402                 buf->flags = m->info->display_picture->flags;
403
404                 hb_list_add( list_raw, buf );
405             }
406         }
407         else if( state == STATE_INVALID )
408         {
409             mpeg2_reset( m->libmpeg2, 0 );
410         }
411
412         /*
413          * Look for Closed Captions if scanning (!job) or if closed captions have been requested.
414          *
415          * Send them on to the closed caption decoder if requested and found.
416          */
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 ) ) 
421         {
422             int i, j;
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 */
426             if (pattern==0x00) 
427                 field1packet=1; /* expect Field 1 second */
428             int capcount=(header[0] & 0x1e) / 2;
429             header++;
430             
431             /*
432              * Add closed captions to the title if we are scanning (no job).
433              *
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
439              * see how it goes).
440              */
441             if( !m->job && m->title )
442             {
443                 hb_subtitle_t * subtitle;
444                 int found = 0;
445                 int i;
446                 
447                 for( i = 0; i < hb_list_count( m->title->list_subtitle ); i++ )
448                 {
449                     subtitle = hb_list_item( m->title->list_subtitle, i);
450                     /*
451                      * Let's call them 608 subs for now even if they aren't, since they 
452                      * are the only types we grok.
453                      */
454                     if( subtitle && subtitle->source == CC608SUB ) 
455                     {
456                         found = 1;
457                         break;
458                     }
459                 }
460                 
461                 if( !found )
462                 {
463                     subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
464                     subtitle->track = 0;
465                     subtitle->id = 0x0;
466                     snprintf( subtitle->lang, sizeof( subtitle->lang ), "Closed Captions");
467                     /*
468                      * The language of the subtitles will be the same as the first audio
469                      * track, i.e. the same as the video.
470                      */
471                     hb_audio_t *audio = hb_list_item( m->title->list_audio, 0 );
472                     if( audio )
473                     {
474                         snprintf( subtitle->iso639_2, sizeof( subtitle->iso639_2 ), 
475                                   audio->config.lang.iso639_2);
476                     } else {
477                         snprintf( subtitle->iso639_2, sizeof( subtitle->iso639_2 ), "und");
478                     }
479                     subtitle->format = TEXTSUB;
480                     subtitle->source = CC608SUB;
481                     subtitle->config.dest   = PASSTHRUSUB;
482                     subtitle->type = 5; 
483                     
484                     hb_list_add( m->title->list_subtitle, subtitle );
485                 }
486             }
487
488             for( i=0; i<capcount; i++ )
489             {
490                 for( j=0; j<2; j++ )
491                 {
492                     uint8_t data[3];
493                     data[0] = header[0];
494                     data[1] = header[1];
495                     data[2] = header[2];
496                     header += 3;
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 )
501                     {
502                         data[0] = 0x04; // Field 1
503                     }   
504                     else
505                     {
506                         data[0] = 0x05; // Field 2
507                     }
508                     hb_mpeg2_cc( m, data );
509                 }
510             }
511             // Deal with extra closed captions some DVD have.
512             while( header[0]==0xfe || header[0]==0xff )
513             {
514                 for( j=0; j<2; j++ )
515                 {
516                     uint8_t data[3];
517                     data[0] = header[0];
518                     data[1] = header[1];
519                     data[2] = header[2];
520                     header += 3;
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 )
525                     {
526                         data[0] = 0x04; // Field 1
527                     }   
528                     else
529                     {
530                         data[0] = 0x05; // Field 2
531                     } 
532                     hb_mpeg2_cc( m, data );
533                 }
534             }   
535         }
536     }
537     return 1;
538 }
539
540 /**********************************************************************
541  * hb_libmpeg2_close
542  **********************************************************************
543  *
544  *********************************************************************/
545 static void hb_libmpeg2_close( hb_libmpeg2_t ** _m )
546 {
547     hb_libmpeg2_t * m = *_m;
548
549     mpeg2_close( m->libmpeg2 );
550
551     free( m );
552     *_m = NULL;
553 }
554
555 /**********************************************************************
556  * The decmpeg2 work object
557  **********************************************************************
558  *
559  *********************************************************************/
560 struct hb_work_private_s
561 {
562     hb_libmpeg2_t * libmpeg2;
563     hb_list_t     * list;
564 };
565
566 /**********************************************************************
567  * hb_work_decmpeg2_init
568  **********************************************************************
569  *
570  *********************************************************************/
571 static int decmpeg2Init( hb_work_object_t * w, hb_job_t * job )
572 {
573     hb_work_private_t * pv;
574
575     pv              = calloc( 1, sizeof( hb_work_private_t ) );
576     w->private_data = pv;
577
578     pv->libmpeg2 = hb_libmpeg2_init();
579     pv->list     = hb_list_init();
580
581     pv->libmpeg2->job = job;
582
583     if( job && job->title ) {
584         pv->libmpeg2->title = job->title;
585     }
586
587     /*
588      * If not scanning, then are we supposed to extract Closed Captions
589      * and send them to the decoder? 
590      */
591     pv->libmpeg2->list_subtitle = hb_list_init();
592     if( job )
593     {
594         hb_subtitle_t * subtitle;
595         int i;
596         
597         for( i = 0; i < hb_list_count( job->list_subtitle ); i++ )
598         {
599             subtitle = hb_list_item( job->list_subtitle, i);
600             if( subtitle && subtitle->source == CC608SUB ) 
601             {
602                 hb_list_add(pv->libmpeg2->list_subtitle, subtitle);
603             }
604         }
605
606     }
607
608     return 0;
609 }
610
611 /**********************************************************************
612  * Work
613  **********************************************************************
614  *
615  *********************************************************************/
616 static int decmpeg2Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
617                          hb_buffer_t ** buf_out )
618 {
619     hb_work_private_t * pv = w->private_data;
620     hb_buffer_t * buf, * last = NULL;
621     int status = HB_WORK_OK;
622
623     if( w->title && pv && pv->libmpeg2 && !pv->libmpeg2->title ) {
624         pv->libmpeg2->title = w->title;
625     }
626
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 )
631     {
632         if ( pv->libmpeg2->job )
633         {
634             pv->libmpeg2->look_for_break = (*buf_in)->new_chap;
635         }
636         (*buf_in)->new_chap = 0;
637     }
638
639     hb_libmpeg2_decode( pv->libmpeg2, *buf_in, pv->list );
640
641     /* if we got an empty buffer signaling end-of-stream send it downstream */
642     if ( (*buf_in)->size == 0 )
643     {
644         int i;
645
646         hb_list_add( pv->list, *buf_in );
647         *buf_in = NULL;
648         status = HB_WORK_DONE;
649         /*
650          * Let the Closed Captions know that it is the end of the data.
651          */
652         for (i = 0; i < hb_list_count( pv->libmpeg2->list_subtitle ); i++)
653         {
654             hb_subtitle_t * subtitle;
655             hb_buffer_t *buf_eof = hb_buffer_init( 0 );
656             
657             subtitle = hb_list_item( pv->libmpeg2->list_subtitle, i );
658             if( buf_eof )
659             {
660                 hb_fifo_push( subtitle->fifo_in, buf_eof );
661             }
662         }
663     }
664
665     *buf_out = NULL;
666     while( ( buf = hb_list_item( pv->list, 0 ) ) )
667     {
668         hb_list_rem( pv->list, buf );
669         if( last )
670         {
671             last->next = buf;
672             last       = buf;
673         }
674         else
675         {
676             *buf_out = buf;
677             last     = buf;
678         }
679     }
680
681     return status;
682 }
683
684 /**********************************************************************
685  * Close
686  **********************************************************************
687  *
688  *********************************************************************/
689 static void decmpeg2Close( hb_work_object_t * w )
690 {
691     hb_work_private_t * pv = w->private_data;
692
693     // don't log during scan
694     if ( pv->libmpeg2->job )
695     {
696         hb_log( "mpeg2 done: %d frames", pv->libmpeg2->nframes );
697     }
698     hb_list_close( &pv->list );
699     hb_list_close( &pv->libmpeg2->list_subtitle );
700     hb_libmpeg2_close( &pv->libmpeg2 );
701     free( pv );
702 }
703
704 static int decmpeg2Info( hb_work_object_t *w, hb_work_info_t *info )
705 {
706     hb_work_private_t *pv = w->private_data;
707
708     memset( info, 0, sizeof(*info) );
709
710     if ( pv && pv->libmpeg2 && pv->libmpeg2->info && pv->libmpeg2->info->sequence )
711     {
712         hb_libmpeg2_t *m = pv->libmpeg2;
713
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;
719
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;
726
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";
731         return 1;
732     }
733     return 0;
734 }
735
736 hb_work_object_t hb_decmpeg2 =
737 {
738     WORK_DECMPEG2,
739     "MPEG-2 decoder (libmpeg2)",
740     decmpeg2Init,
741     decmpeg2Work,
742     decmpeg2Close,
743     decmpeg2Info
744 };
745