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
42 double duration; // frame duration (for video)
47 /***********************************************************************
48 * hb_work_decavcodec_init
49 ***********************************************************************
51 **********************************************************************/
52 static int decavcodecInit( hb_work_object_t * w, hb_job_t * job )
56 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
61 int codec_id = w->codec_param;
64 codec_id = CODEC_ID_MP2;
65 codec = avcodec_find_decoder( codec_id );
66 pv->parser = av_parser_init( codec_id );
68 pv->context = avcodec_alloc_context();
69 avcodec_open( pv->context, codec );
74 /***********************************************************************
76 ***********************************************************************
78 **********************************************************************/
79 static void decavcodecClose( hb_work_object_t * w )
81 hb_work_private_t * pv = w->private_data;
84 av_parser_close(pv->parser);
86 if ( pv->context && pv->context->codec )
88 avcodec_close( pv->context );
92 hb_list_close( &pv->list );
96 /***********************************************************************
98 ***********************************************************************
100 **********************************************************************/
101 static int decavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
102 hb_buffer_t ** buf_out )
104 hb_work_private_t * pv = w->private_data;
105 hb_buffer_t * in = *buf_in, * buf, * last = NULL;
106 int pos, len, out_size, i, uncompressed_len;
107 short buffer[AVCODEC_MAX_AUDIO_FRAME_SIZE];
109 unsigned char *parser_output_buffer;
110 int parser_output_buffer_len;
114 cur = ( in->start < 0 )? pv->pts_next : in->start;
117 while( pos < in->size )
119 len = av_parser_parse( pv->parser, pv->context,
120 &parser_output_buffer, &parser_output_buffer_len,
121 in->data + pos, in->size - pos, cur, cur );
123 uncompressed_len = 0;
124 if (parser_output_buffer_len)
126 out_size = sizeof(buffer);
127 uncompressed_len = avcodec_decode_audio2( pv->context, buffer,
129 parser_output_buffer,
130 parser_output_buffer_len );
137 buf = hb_buffer_init( 2 * out_size );
139 int sample_size_in_bytes = 2; // Default to 2 bytes
140 switch (pv->context->sample_fmt)
143 sample_size_in_bytes = 2;
145 /* We should handle other formats here - but that needs additional format conversion work below */
146 /* For now we'll just report the error and try to carry on */
148 hb_log("decavcodecWork - Unknown Sample Format from avcodec_decode_audio (%d) !", pv->context->sample_fmt);
153 buf->stop = cur + 90000 * ( out_size / (sample_size_in_bytes * pv->context->channels) ) /
154 pv->context->sample_rate;
158 fl32 = (float *) buf->data;
159 for( i = 0; i < out_size / 2; i++ )
166 last = last->next = buf;
170 *buf_out = last = buf;
182 static int decavcodecInfo( hb_work_object_t *w, hb_work_info_t *info )
184 hb_work_private_t *pv = w->private_data;
186 memset( info, 0, sizeof(*info) );
188 if ( pv && pv->context )
190 AVCodecContext *context = pv->context;
191 info->bitrate = context->bit_rate;
192 info->rate = context->time_base.num;
193 info->rate_base = context->time_base.den;
194 info->profile = context->profile;
195 info->level = context->level;
201 static int decavcodecBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
202 hb_work_info_t *info )
204 hb_work_private_t *pv = w->private_data;
206 memset( info, 0, sizeof(*info) );
208 if ( pv && pv->context )
210 return decavcodecInfo( w, info );
213 // We should parse the bitstream to find its parameters but for right
214 // now we just return dummy values if there's a codec that will handle it.
215 AVCodec *codec = avcodec_find_decoder( w->codec_param? w->codec_param :
219 static char codec_name[64];
221 info->name = strncpy( codec_name, codec->name, sizeof(codec_name)-1 );
222 info->bitrate = 384000;
225 info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
231 /* -------------------------------------------------------------
232 * General purpose video decoder using libavcodec
235 static uint8_t *copy_plane( uint8_t *dst, uint8_t* src, int dstride, int sstride,
238 if ( dstride == sstride )
240 memcpy( dst, src, dstride * h );
241 return dst + dstride * h;
243 int lbytes = dstride <= sstride? dstride : sstride;
246 memcpy( dst, src, lbytes );
253 /* Note: assumes frame format is PIX_FMT_YUV420P */
254 static hb_buffer_t *copy_frame( AVCodecContext *context, AVFrame *frame )
256 int w = context->width, h = context->height;
257 hb_buffer_t *buf = hb_buffer_init( w * h * 3 / 2 );
258 uint8_t *dst = buf->data;
260 dst = copy_plane( dst, frame->data[0], w, frame->linesize[0], h );
262 dst = copy_plane( dst, frame->data[1], w, frame->linesize[1], h );
263 dst = copy_plane( dst, frame->data[2], w, frame->linesize[2], h );
268 static int get_frame_buf( AVCodecContext *context, AVFrame *frame )
270 hb_work_private_t *pv = context->opaque;
271 frame->pts = pv->pts;
274 return avcodec_default_get_buffer( context, frame );
277 static void log_chapter( hb_work_private_t *pv, int chap_num, int64_t pts )
279 hb_chapter_t *c = hb_list_item( pv->job->title->list_chapter, chap_num - 1 );
280 hb_log( "%s: \"%s\" (%d) at frame %u time %lld", pv->context->codec->name,
281 c->title, chap_num, pv->nframes, pts );
284 static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
289 avcodec_decode_video( pv->context, &frame, &got_picture, data, size );
292 // ffmpeg makes it hard to attach a pts to a frame. if the MPEG ES
293 // packet had a pts we handed it to av_parser_parse (if the packet had
294 // no pts we set it to -1 but before the parse we can't distinguish between
295 // the start of a video frame with no pts & an intermediate packet of
296 // some frame which never has a pts). we hope that when parse returns
297 // the frame to us the pts we originally handed it will be in parser->pts.
298 // we put this pts into pv->pts so that when a avcodec_decode_video
299 // finally gets around to allocating an AVFrame to hold the decoded
300 // frame we can stuff that pts into the frame. if all of these relays
301 // worked at this point frame.pts should hold the frame's pts from the
302 // original data stream or -1 if it didn't have one. in the latter case
303 // we generate the next pts in sequence for it.
304 double pts = frame.pts;
309 if ( pv->duration == 0 )
311 pv->duration = 90000. * pv->context->time_base.num /
312 pv->context->time_base.den;
314 double frame_dur = pv->duration;
315 frame_dur += frame.repeat_pict * frame_dur * 0.5;
316 pv->pts_next = pts + frame_dur;
318 hb_buffer_t *buf = copy_frame( pv->context, &frame );
321 if ( pv->new_chap && buf->start >= pv->chap_time )
323 buf->new_chap = pv->new_chap;
328 log_chapter( pv, buf->new_chap, buf->start );
331 else if ( pv->job && pv->nframes == 0 )
333 log_chapter( pv, pv->job->chapter_start, buf->start );
335 hb_list_add( pv->list, buf );
341 static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size,
342 int64_t pts, int64_t dts )
345 * The following loop is a do..while because we need to handle both
346 * data & the flush at the end (signaled by size=0). At the end there's
347 * generally a frame in the parser & one or more frames in the decoder
348 * (depending on the bframes setting).
354 int len = av_parser_parse( pv->parser, pv->context, &pout, &pout_len,
355 data + pos, size - pos, pts, dts );
360 pv->pts = pv->parser->pts;
361 decodeFrame( pv, pout, pout_len );
363 } while ( pos < size );
365 /* the stuff above flushed the parser, now flush the decoder */
366 while ( size == 0 && decodeFrame( pv, NULL, 0 ) )
371 static hb_buffer_t *link_buf_list( hb_work_private_t *pv )
373 hb_buffer_t *head = hb_list_item( pv->list, 0 );
377 hb_list_rem( pv->list, head );
379 hb_buffer_t *last = head, *buf;
381 while ( ( buf = hb_list_item( pv->list, 0 ) ) != NULL )
383 hb_list_rem( pv->list, buf );
392 static int decavcodecvInit( hb_work_object_t * w, hb_job_t * job )
395 hb_work_private_t *pv = calloc( 1, sizeof( hb_work_private_t ) );
396 w->private_data = pv;
398 pv->list = hb_list_init();
400 int codec_id = w->codec_param;
401 pv->parser = av_parser_init( codec_id );
402 pv->context = avcodec_alloc_context2( CODEC_TYPE_VIDEO );
404 /* we have to wrap ffmpeg's get_buffer to be able to set the pts (?!) */
405 pv->context->opaque = pv;
406 pv->context->get_buffer = get_frame_buf;
408 AVCodec *codec = avcodec_find_decoder( codec_id );
410 // we can't call the avstream funcs but the read_header func in the
411 // AVInputFormat may set up some state in the AVContext. In particular
412 // vc1t_read_header allocates 'extradata' to deal with header issues
413 // related to Microsoft's bizarre engineering notions. We alloc a chunk
414 // of space to make vc1 work then associate the codec with the context.
415 pv->context->extradata_size = 32;
416 pv->context->extradata = av_malloc(pv->context->extradata_size);
417 avcodec_open( pv->context, codec );
422 static int decavcodecvWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
423 hb_buffer_t ** buf_out )
425 hb_work_private_t *pv = w->private_data;
426 hb_buffer_t *in = *buf_in;
432 /* if we got an empty buffer signaling end-of-stream send it downstream */
435 decodeVideo( pv, in->data, in->size, pts, dts );
436 hb_list_add( pv->list, in );
437 *buf_out = link_buf_list( pv );
438 hb_log( "%s done: %d frames", pv->context->codec->name, pv->nframes );
445 dts = in->renderOffset;
449 pv->new_chap = in->new_chap;
450 pv->chap_time = pts >= 0? pts : pv->pts_next;
452 decodeVideo( pv, in->data, in->size, pts, dts );
453 hb_buffer_close( &in );
454 *buf_out = link_buf_list( pv );
458 static int decavcodecvInfo( hb_work_object_t *w, hb_work_info_t *info )
460 hb_work_private_t *pv = w->private_data;
462 memset( info, 0, sizeof(*info) );
464 if ( pv && pv->context )
466 AVCodecContext *context = pv->context;
467 info->bitrate = context->bit_rate;
468 info->width = context->width;
469 info->height = context->height;
471 /* ffmpeg gives the frame rate in frames per second while HB wants
472 * it in units of the 27MHz MPEG clock. */
473 info->rate = 27000000;
474 info->rate_base = (int64_t)context->time_base.num * 27000000LL /
475 context->time_base.den;
477 /* ffmpeg returns the Pixel Aspect Ratio (PAR). Handbrake wants the
478 * Display Aspect Ratio so we convert by scaling by the Storage
479 * Aspect Ratio (w/h). We do the calc in floating point to get the
480 * rounding right. We round in the second decimal digit because we
481 * scale the (integer) aspect by 9 to preserve the 1st digit. */
482 info->aspect = ( (double)context->sample_aspect_ratio.num *
483 (double)context->width /
484 (double)context->sample_aspect_ratio.den /
485 (double)context->height + 0.05 ) * HB_ASPECT_BASE;
487 if( context->sample_aspect_ratio.num == 0 )
489 info->aspect = (double)context->width / (double)context->height * HB_ASPECT_BASE;
491 info->profile = context->profile;
492 info->level = context->level;
493 info->name = context->codec->name;
499 static int decavcodecvBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
500 hb_work_info_t *info )
505 hb_work_object_t hb_decavcodecv =
508 "Video decoder (libavcodec)",
517 // This is a special decoder for ffmpeg streams. The ffmpeg stream reader
518 // includes a parser and passes information from the parser to the decoder
519 // via a codec context kept in the AVStream of the reader's AVFormatContext.
520 // We *have* to use that codec context to decode the stream or we'll get
521 // garbage. ffmpeg_title_scan put a cookie that can be used to get to that
522 // codec context in our codec_param.
524 // this routine gets the appropriate context pointer from the ffmpeg
525 // stream reader. it can't be called until we get the first buffer because
526 // we can't guarantee that reader will be called before the our init
527 // routine and if our init is called first we'll get a pointer to the
528 // old scan stream (which has already been closed).
529 static void init_ffmpeg_context( hb_work_object_t *w )
531 hb_work_private_t *pv = w->private_data;
532 pv->context = hb_ffmpeg_context( w->codec_param );
534 // during scan the decoder gets closed & reopened which will
535 // close the codec so reopen it if it's not there
536 if ( ! pv->context->codec )
538 AVCodec *codec = avcodec_find_decoder( pv->context->codec_id );
539 avcodec_open( pv->context, codec );
541 // set up our best guess at the frame duration.
542 // the frame rate in the codec seems to be bogus but it's ok in the stream.
543 AVStream *st = hb_ffmpeg_avstream( w->codec_param );
544 AVRational tb = st->time_base;
545 if ( st->r_frame_rate.den && st->r_frame_rate.num )
547 tb.num = st->r_frame_rate.den;
548 tb.den = st->r_frame_rate.num;
550 pv->duration = 90000. * tb.num / tb.den;
552 // we have to wrap ffmpeg's get_buffer to be able to set the pts (?!)
553 pv->context->opaque = pv;
554 pv->context->get_buffer = get_frame_buf;
557 static void prepare_ffmpeg_buffer( hb_buffer_t * in )
559 // ffmpeg requires an extra 8 bytes of zero at the end of the buffer and
560 // will seg fault in odd, data dependent ways if it's not there. (my guess
561 // is this is a case of a local performance optimization creating a global
562 // performance degradation since all the time wasted by extraneous data
563 // copies & memory zeroing has to be huge compared to the minor reduction
564 // in inner-loop instructions this affords - modern cpus bottleneck on
565 // memory bandwidth not instruction bandwidth).
566 if ( in->size + FF_INPUT_BUFFER_PADDING_SIZE > in->alloc )
568 // have to realloc to add the padding
569 hb_buffer_realloc( in, in->size + FF_INPUT_BUFFER_PADDING_SIZE );
571 memset( in->data + in->size, 0, FF_INPUT_BUFFER_PADDING_SIZE );
574 static int decavcodecviInit( hb_work_object_t * w, hb_job_t * job )
577 hb_work_private_t *pv = calloc( 1, sizeof( hb_work_private_t ) );
578 w->private_data = pv;
580 pv->list = hb_list_init();
585 static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
586 hb_buffer_t ** buf_out )
588 hb_work_private_t *pv = w->private_data;
591 init_ffmpeg_context( w );
593 switch ( pv->context->codec_id )
595 // These are the only formats whose timestamps we'll believe.
596 // All others are treated as CFR (i.e., we take the first timestamp
597 // then generate all the others from the frame rate). The reason for
598 // this is that the M$ encoders are so frigging buggy with garbage
599 // like packed b-frames (vfw divx mpeg4) that believing their timestamps
600 // results in discarding more than half the video frames because they'll
601 // be out of sequence (and attempting to reseqence them doesn't work
602 // because it's the timestamps that are wrong, not the decoded frame
603 // order). All hail Redmond, ancestral home of the rich & stupid.
604 case CODEC_ID_MPEG2VIDEO:
605 case CODEC_ID_RAWVIDEO:
615 hb_buffer_t *in = *buf_in;
620 /* if we got an empty buffer signaling end-of-stream send it downstream */
623 /* flush any frames left in the decoder */
624 while ( decodeFrame( pv, NULL, 0 ) )
627 hb_list_add( pv->list, in );
628 *buf_out = link_buf_list( pv );
629 hb_log( "%s done: %d frames %d drops", pv->context->codec->name,
630 pv->nframes, pv->ndrops );
636 // use the first timestamp as our 'next expected' pts
637 if ( pv->pts_next <= 0 )
639 pv->pts_next = in->start;
642 if ( ! pv->ignore_pts )
647 hb_log( "overwriting pts %lld with %lld (diff %d)",
648 pv->pts, pts, pts - pv->pts );
650 if ( pv->pts_next - pts >= pv->duration )
652 // this frame starts more than a frame time before where
653 // the nominal frame rate says it should - drop it.
654 // log the first 10 drops so we'll know what's going on.
655 if ( pv->ndrops++ < 10 )
657 hb_log( "time reversal next %.0f pts %lld (diff %g)",
658 pv->pts_next, pts, pv->pts_next - pts );
660 hb_buffer_close( &in );
669 pv->new_chap = in->new_chap;
670 pv->chap_time = pts >= 0? pts : pv->pts_next;
672 prepare_ffmpeg_buffer( in );
673 decodeFrame( pv, in->data, in->size );
674 hb_buffer_close( &in );
675 *buf_out = link_buf_list( pv );
679 static int decavcodecviInfo( hb_work_object_t *w, hb_work_info_t *info )
681 if ( decavcodecvInfo( w, info ) )
683 // the frame rate in the codec seems to be bogus but it's ok in the stream.
684 AVStream *st = hb_ffmpeg_avstream( w->codec_param );
686 if ( st->r_frame_rate.den && st->r_frame_rate.num )
688 tb.num = st->r_frame_rate.den;
689 tb.den = st->r_frame_rate.num;
696 // ffmpeg gives the frame rate in frames per second while HB wants
697 // it in units of the 27MHz MPEG clock. */
698 info->rate = 27000000;
699 info->rate_base = (int64_t)tb.num * 27000000LL / tb.den;
705 static void decodeAudio( hb_work_private_t *pv, uint8_t *data, int size )
707 AVCodecContext *context = pv->context;
712 int16_t buffer[AVCODEC_MAX_AUDIO_FRAME_SIZE];
713 int out_size = sizeof(buffer);
714 int len = avcodec_decode_audio2( context, buffer, &out_size,
715 data + pos, size - pos );
723 hb_buffer_t *buf = hb_buffer_init( 2 * out_size );
725 double pts = pv->pts_next;
728 pts += out_size * pv->duration;
732 float *fl32 = (float *)buf->data;
734 for( i = 0; i < out_size; ++i )
738 hb_list_add( pv->list, buf );
743 static int decavcodecaiWork( hb_work_object_t *w, hb_buffer_t **buf_in,
744 hb_buffer_t **buf_out )
746 hb_work_private_t *pv = w->private_data;
749 init_ffmpeg_context( w );
750 pv->duration = 90000. /
751 (double)( pv->context->sample_rate * pv->context->channels );
753 hb_buffer_t *in = *buf_in;
755 if ( in->start >= 0 &&
756 ( pv->pts_next < 0 || ( in->start - pv->pts_next ) > 90*100 ) )
758 pv->pts_next = in->start;
760 prepare_ffmpeg_buffer( in );
761 decodeAudio( pv, in->data, in->size );
762 *buf_out = link_buf_list( pv );
767 hb_work_object_t hb_decavcodecvi =
770 "Video decoder (ffmpeg streams)",
778 hb_work_object_t hb_decavcodecai =
781 "Audio decoder (ffmpeg streams)",