1 /* $Id: decavcodec.c,v 1.6 2005/03/06 04:08:54 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 "libavcodec/avcodec.h"
10 #include "libavformat/avformat.h"
12 static int decavcodecInit( hb_work_object_t *, hb_job_t * );
13 static int decavcodecWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
14 static void decavcodecClose( hb_work_object_t * );
15 static int decavcodecInfo( hb_work_object_t *, hb_work_info_t * );
16 static int decavcodecBSInfo( hb_work_object_t *, const hb_buffer_t *, hb_work_info_t * );
18 hb_work_object_t hb_decavcodec =
21 "MPGA decoder (libavcodec)",
29 struct hb_work_private_s
32 AVCodecContext *context;
33 AVCodecParserContext *parser;
35 double pts_next; // next pts we expect to generate
36 int64_t pts; // (video) pts passing from parser to decoder
37 int64_t chap_time; // time of next chap mark (if new_chap != 0)
39 int ignore_pts; // workaround M$ bugs
41 double duration; // frame duration (for video)
46 /***********************************************************************
47 * hb_work_decavcodec_init
48 ***********************************************************************
50 **********************************************************************/
51 static int decavcodecInit( hb_work_object_t * w, hb_job_t * job )
55 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
60 int codec_id = w->codec_param;
63 codec_id = CODEC_ID_MP2;
64 codec = avcodec_find_decoder( codec_id );
65 pv->parser = av_parser_init( codec_id );
67 pv->context = avcodec_alloc_context();
68 avcodec_open( pv->context, codec );
73 /***********************************************************************
75 ***********************************************************************
77 **********************************************************************/
78 static void decavcodecClose( hb_work_object_t * w )
80 hb_work_private_t * pv = w->private_data;
83 av_parser_close(pv->parser);
85 if ( pv->context && pv->context->codec )
87 avcodec_close( pv->context );
91 hb_list_close( &pv->list );
95 /***********************************************************************
97 ***********************************************************************
99 **********************************************************************/
100 static int decavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
101 hb_buffer_t ** buf_out )
103 hb_work_private_t * pv = w->private_data;
104 hb_buffer_t * in = *buf_in, * buf, * last = NULL;
105 int pos, len, out_size, i, uncompressed_len;
106 short buffer[AVCODEC_MAX_AUDIO_FRAME_SIZE];
108 unsigned char *parser_output_buffer;
109 int parser_output_buffer_len;
113 cur = ( in->start < 0 )? pv->pts_next : in->start;
116 while( pos < in->size )
118 len = av_parser_parse( pv->parser, pv->context,
119 &parser_output_buffer, &parser_output_buffer_len,
120 in->data + pos, in->size - pos, cur, cur );
122 uncompressed_len = 0;
123 if (parser_output_buffer_len)
125 out_size = sizeof(buffer);
126 uncompressed_len = avcodec_decode_audio2( pv->context, buffer,
128 parser_output_buffer,
129 parser_output_buffer_len );
136 buf = hb_buffer_init( 2 * out_size );
138 int sample_size_in_bytes = 2; // Default to 2 bytes
139 switch (pv->context->sample_fmt)
142 sample_size_in_bytes = 2;
144 /* We should handle other formats here - but that needs additional format conversion work below */
145 /* For now we'll just report the error and try to carry on */
147 hb_log("decavcodecWork - Unknown Sample Format from avcodec_decode_audio (%d) !", pv->context->sample_fmt);
152 buf->stop = cur + 90000 * ( out_size / (sample_size_in_bytes * pv->context->channels) ) /
153 pv->context->sample_rate;
157 fl32 = (float *) buf->data;
158 for( i = 0; i < out_size / 2; i++ )
165 last = last->next = buf;
169 *buf_out = last = buf;
181 static int decavcodecInfo( hb_work_object_t *w, hb_work_info_t *info )
183 hb_work_private_t *pv = w->private_data;
185 memset( info, 0, sizeof(*info) );
187 if ( pv && pv->context )
189 AVCodecContext *context = pv->context;
190 info->bitrate = context->bit_rate;
191 info->rate = context->time_base.num;
192 info->rate_base = context->time_base.den;
193 info->profile = context->profile;
194 info->level = context->level;
200 static int decavcodecBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
201 hb_work_info_t *info )
203 hb_work_private_t *pv = w->private_data;
205 memset( info, 0, sizeof(*info) );
207 if ( pv && pv->context )
209 return decavcodecInfo( w, info );
212 // We should parse the bitstream to find its parameters but for right
213 // now we just return dummy values if there's a codec that will handle it.
214 AVCodec *codec = avcodec_find_decoder( w->codec_param? w->codec_param :
218 static char codec_name[64];
220 info->name = strncpy( codec_name, codec->name, sizeof(codec_name)-1 );
221 info->bitrate = 384000;
224 info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
230 /* -------------------------------------------------------------
231 * General purpose video decoder using libavcodec
234 static uint8_t *copy_plane( uint8_t *dst, uint8_t* src, int dstride, int sstride,
237 if ( dstride == sstride )
239 memcpy( dst, src, dstride * h );
240 return dst + dstride * h;
242 int lbytes = dstride <= sstride? dstride : sstride;
245 memcpy( dst, src, lbytes );
252 /* Note: assumes frame format is PIX_FMT_YUV420P */
253 static hb_buffer_t *copy_frame( AVCodecContext *context, AVFrame *frame )
255 int w = context->width, h = context->height;
256 hb_buffer_t *buf = hb_buffer_init( w * h * 3 / 2 );
257 uint8_t *dst = buf->data;
259 dst = copy_plane( dst, frame->data[0], w, frame->linesize[0], h );
261 dst = copy_plane( dst, frame->data[1], w, frame->linesize[1], h );
262 dst = copy_plane( dst, frame->data[2], w, frame->linesize[2], h );
267 static int get_frame_buf( AVCodecContext *context, AVFrame *frame )
269 hb_work_private_t *pv = context->opaque;
270 frame->pts = pv->pts;
273 return avcodec_default_get_buffer( context, frame );
276 static void log_chapter( hb_work_private_t *pv, int chap_num, int64_t pts )
278 hb_chapter_t *c = hb_list_item( pv->job->title->list_chapter, chap_num - 1 );
279 hb_log( "%s: \"%s\" (%d) at frame %u time %lld", pv->context->codec->name,
280 c->title, chap_num, pv->nframes, pts );
283 static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
288 avcodec_decode_video( pv->context, &frame, &got_picture, data, size );
291 // ffmpeg makes it hard to attach a pts to a frame. if the MPEG ES
292 // packet had a pts we handed it to av_parser_parse (if the packet had
293 // no pts we set it to -1 but before the parse we can't distinguish between
294 // the start of a video frame with no pts & an intermediate packet of
295 // some frame which never has a pts). we hope that when parse returns
296 // the frame to us the pts we originally handed it will be in parser->pts.
297 // we put this pts into pv->pts so that when a avcodec_decode_video
298 // finally gets around to allocating an AVFrame to hold the decoded
299 // frame we can stuff that pts into the frame. if all of these relays
300 // worked at this point frame.pts should hold the frame's pts from the
301 // original data stream or -1 if it didn't have one. in the latter case
302 // we generate the next pts in sequence for it.
303 double pts = frame.pts;
308 if ( pv->duration == 0 )
310 pv->duration = 90000. * pv->context->time_base.num /
311 pv->context->time_base.den;
313 double frame_dur = pv->duration;
314 frame_dur += frame.repeat_pict * frame_dur * 0.5;
315 pv->pts_next = pts + frame_dur;
317 hb_buffer_t *buf = copy_frame( pv->context, &frame );
320 if ( pv->new_chap && buf->start >= pv->chap_time )
322 buf->new_chap = pv->new_chap;
327 log_chapter( pv, buf->new_chap, buf->start );
330 else if ( pv->job && pv->nframes == 0 )
332 log_chapter( pv, pv->job->chapter_start, buf->start );
334 hb_list_add( pv->list, buf );
340 static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size,
341 int64_t pts, int64_t dts )
344 * The following loop is a do..while because we need to handle both
345 * data & the flush at the end (signaled by size=0). At the end there's
346 * generally a frame in the parser & one or more frames in the decoder
347 * (depending on the bframes setting).
353 int len = av_parser_parse( pv->parser, pv->context, &pout, &pout_len,
354 data + pos, size - pos, pts, dts );
359 pv->pts = pv->parser->pts;
360 decodeFrame( pv, pout, pout_len );
362 } while ( pos < size );
364 /* the stuff above flushed the parser, now flush the decoder */
365 while ( size == 0 && decodeFrame( pv, NULL, 0 ) )
370 static hb_buffer_t *link_buf_list( hb_work_private_t *pv )
372 hb_buffer_t *head = hb_list_item( pv->list, 0 );
376 hb_list_rem( pv->list, head );
378 hb_buffer_t *last = head, *buf;
380 while ( ( buf = hb_list_item( pv->list, 0 ) ) != NULL )
382 hb_list_rem( pv->list, buf );
391 static int decavcodecvInit( hb_work_object_t * w, hb_job_t * job )
394 hb_work_private_t *pv = calloc( 1, sizeof( hb_work_private_t ) );
395 w->private_data = pv;
397 pv->list = hb_list_init();
399 int codec_id = w->codec_param;
400 pv->parser = av_parser_init( codec_id );
401 pv->context = avcodec_alloc_context2( CODEC_TYPE_VIDEO );
403 /* we have to wrap ffmpeg's get_buffer to be able to set the pts (?!) */
404 pv->context->opaque = pv;
405 pv->context->get_buffer = get_frame_buf;
407 AVCodec *codec = avcodec_find_decoder( codec_id );
409 // we can't call the avstream funcs but the read_header func in the
410 // AVInputFormat may set up some state in the AVContext. In particular
411 // vc1t_read_header allocates 'extradata' to deal with header issues
412 // related to Microsoft's bizarre engineering notions. We alloc a chunk
413 // of space to make vc1 work then associate the codec with the context.
414 pv->context->extradata_size = 32;
415 pv->context->extradata = av_malloc(pv->context->extradata_size);
416 avcodec_open( pv->context, codec );
421 static int decavcodecvWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
422 hb_buffer_t ** buf_out )
424 hb_work_private_t *pv = w->private_data;
425 hb_buffer_t *in = *buf_in;
431 /* if we got an empty buffer signaling end-of-stream send it downstream */
434 decodeVideo( pv, in->data, in->size, pts, dts );
435 hb_list_add( pv->list, in );
436 *buf_out = link_buf_list( pv );
437 hb_log( "%s done: %d frames", pv->context->codec->name, pv->nframes );
444 dts = in->renderOffset;
448 pv->new_chap = in->new_chap;
449 pv->chap_time = pts >= 0? pts : pv->pts_next;
451 decodeVideo( pv, in->data, in->size, pts, dts );
452 hb_buffer_close( &in );
453 *buf_out = link_buf_list( pv );
457 static int decavcodecvInfo( hb_work_object_t *w, hb_work_info_t *info )
459 hb_work_private_t *pv = w->private_data;
461 memset( info, 0, sizeof(*info) );
463 if ( pv && pv->context )
465 AVCodecContext *context = pv->context;
466 info->bitrate = context->bit_rate;
467 info->width = context->width;
468 info->height = context->height;
470 /* ffmpeg gives the frame rate in frames per second while HB wants
471 * it in units of the 27MHz MPEG clock. */
472 info->rate = 27000000;
473 info->rate_base = (int64_t)context->time_base.num * 27000000LL /
474 context->time_base.den;
476 /* ffmpeg returns the Pixel Aspect Ratio (PAR). Handbrake wants the
477 * Display Aspect Ratio so we convert by scaling by the Storage
478 * Aspect Ratio (w/h). We do the calc in floating point to get the
479 * rounding right. We round in the second decimal digit because we
480 * scale the (integer) aspect by 9 to preserve the 1st digit. */
481 info->aspect = ( (double)context->sample_aspect_ratio.num *
482 (double)context->width /
483 (double)context->sample_aspect_ratio.den /
484 (double)context->height + 0.05 ) * HB_ASPECT_BASE;
486 if( context->sample_aspect_ratio.num == 0 )
488 info->aspect = (double)context->width / (double)context->height * HB_ASPECT_BASE;
490 info->profile = context->profile;
491 info->level = context->level;
492 info->name = context->codec->name;
498 static int decavcodecvBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
499 hb_work_info_t *info )
504 hb_work_object_t hb_decavcodecv =
507 "Video decoder (libavcodec)",
516 // This is a special decoder for ffmpeg streams. The ffmpeg stream reader
517 // includes a parser and passes information from the parser to the decoder
518 // via a codec context kept in the AVStream of the reader's AVFormatContext.
519 // We *have* to use that codec context to decode the stream or we'll get
520 // garbage. ffmpeg_title_scan put a cookie that can be used to get to that
521 // codec context in our codec_param.
523 // this routine gets the appropriate context pointer from the ffmpeg
524 // stream reader. it can't be called until we get the first buffer because
525 // we can't guarantee that reader will be called before the our init
526 // routine and if our init is called first we'll get a pointer to the
527 // old scan stream (which has already been closed).
528 static void init_ffmpeg_context( hb_work_object_t *w )
530 hb_work_private_t *pv = w->private_data;
531 pv->context = hb_ffmpeg_context( w->codec_param );
533 // during scan the decoder gets closed & reopened which will
534 // close the codec so reopen it if it's not there
535 if ( ! pv->context->codec )
537 AVCodec *codec = avcodec_find_decoder( pv->context->codec_id );
538 avcodec_open( pv->context, codec );
540 // set up our best guess at the frame duration.
541 // the frame rate in the codec seems to be bogus but it's ok in the stream.
542 AVStream *st = hb_ffmpeg_avstream( w->codec_param );
543 AVRational tb = st->time_base;
544 if ( st->r_frame_rate.den && st->r_frame_rate.num )
546 tb.num = st->r_frame_rate.den;
547 tb.den = st->r_frame_rate.num;
549 pv->duration = 90000. * tb.num / tb.den;
551 // we have to wrap ffmpeg's get_buffer to be able to set the pts (?!)
552 pv->context->opaque = pv;
553 pv->context->get_buffer = get_frame_buf;
556 static void prepare_ffmpeg_buffer( hb_buffer_t * in )
558 // ffmpeg requires an extra 8 bytes of zero at the end of the buffer and
559 // will seg fault in odd, data dependent ways if it's not there. (my guess
560 // is this is a case of a local performance optimization creating a global
561 // performance degradation since all the time wasted by extraneous data
562 // copies & memory zeroing has to be huge compared to the minor reduction
563 // in inner-loop instructions this affords - modern cpus bottleneck on
564 // memory bandwidth not instruction bandwidth).
565 if ( in->size + FF_INPUT_BUFFER_PADDING_SIZE > in->alloc )
567 // have to realloc to add the padding
568 hb_buffer_realloc( in, in->size + FF_INPUT_BUFFER_PADDING_SIZE );
570 memset( in->data + in->size, 0, FF_INPUT_BUFFER_PADDING_SIZE );
573 static int decavcodecviInit( hb_work_object_t * w, hb_job_t * job )
576 hb_work_private_t *pv = calloc( 1, sizeof( hb_work_private_t ) );
577 w->private_data = pv;
579 pv->list = hb_list_init();
584 static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
585 hb_buffer_t ** buf_out )
587 hb_work_private_t *pv = w->private_data;
590 init_ffmpeg_context( w );
592 switch ( pv->context->codec_id )
594 // These are the only formats whose timestamps we'll believe.
595 // All others are treated as CFR (i.e., we take the first timestamp
596 // then generate all the others from the frame rate). The reason for
597 // this is that the M$ encoders are so frigging buggy with garbage
598 // like packed b-frames (vfw divx mpeg4) that believing their timestamps
599 // results in discarding more than half the video frames because they'll
600 // be out of sequence (and attempting to reseqence them doesn't work
601 // because it's the timestamps that are wrong, not the decoded frame
602 // order). All hail Redmond, ancestral home of the rich & stupid.
603 case CODEC_ID_MPEG2VIDEO:
604 case CODEC_ID_RAWVIDEO:
614 hb_buffer_t *in = *buf_in;
619 /* if we got an empty buffer signaling end-of-stream send it downstream */
622 /* flush any frames left in the decoder */
623 while ( decodeFrame( pv, NULL, 0 ) )
626 hb_list_add( pv->list, in );
627 *buf_out = link_buf_list( pv );
628 hb_log( "%s done: %d frames", pv->context->codec->name, pv->nframes );
634 // use the first timestamp as our 'next expected' pts
635 if ( pv->pts_next <= 0 )
637 pv->pts_next = in->start;
640 if ( ! pv->ignore_pts )
645 hb_log( "overwriting pts %lld with %lld (diff %d)",
646 pv->pts, pts, pts - pv->pts );
648 if ( pv->pts_next - pts >= 10.)
650 hb_log( "time reversal next %.0f pts %lld (diff %g)",
651 pv->pts_next, pts, pv->pts_next - pts );
659 pv->new_chap = in->new_chap;
660 pv->chap_time = pts >= 0? pts : pv->pts_next;
662 prepare_ffmpeg_buffer( in );
663 decodeFrame( pv, in->data, in->size );
664 hb_buffer_close( &in );
665 *buf_out = link_buf_list( pv );
669 static int decavcodecviInfo( hb_work_object_t *w, hb_work_info_t *info )
671 if ( decavcodecvInfo( w, info ) )
673 // the frame rate in the codec seems to be bogus but it's ok in the stream.
674 AVStream *st = hb_ffmpeg_avstream( w->codec_param );
676 if ( st->r_frame_rate.den && st->r_frame_rate.num )
678 tb.num = st->r_frame_rate.den;
679 tb.den = st->r_frame_rate.num;
686 // ffmpeg gives the frame rate in frames per second while HB wants
687 // it in units of the 27MHz MPEG clock. */
688 info->rate = 27000000;
689 info->rate_base = (int64_t)tb.num * 27000000LL / tb.den;
695 static void decodeAudio( hb_work_private_t *pv, uint8_t *data, int size )
697 AVCodecContext *context = pv->context;
702 int16_t buffer[AVCODEC_MAX_AUDIO_FRAME_SIZE];
703 int out_size = sizeof(buffer);
704 int len = avcodec_decode_audio2( context, buffer, &out_size,
705 data + pos, size - pos );
713 hb_buffer_t *buf = hb_buffer_init( 2 * out_size );
715 double pts = pv->pts_next;
718 pts += out_size * pv->duration;
722 float *fl32 = (float *)buf->data;
724 for( i = 0; i < out_size; ++i )
728 hb_list_add( pv->list, buf );
733 static int decavcodecaiWork( hb_work_object_t *w, hb_buffer_t **buf_in,
734 hb_buffer_t **buf_out )
736 hb_work_private_t *pv = w->private_data;
739 init_ffmpeg_context( w );
740 pv->duration = 90000. /
741 (double)( pv->context->sample_rate * pv->context->channels );
743 hb_buffer_t *in = *buf_in;
745 if ( in->start >= 0 &&
746 ( pv->pts_next < 0 || ( in->start - pv->pts_next ) > 90*100 ) )
748 pv->pts_next = in->start;
750 prepare_ffmpeg_buffer( in );
751 decodeAudio( pv, in->data, in->size );
752 *buf_out = link_buf_list( pv );
757 hb_work_object_t hb_decavcodecvi =
760 "Video decoder (ffmpeg streams)",
768 hb_work_object_t hb_decavcodecai =
771 "Audio decoder (ffmpeg streams)",