1 /* $Id: encavcodec.c,v 1.23 2005/10/13 23:47:06 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.m0k.org/>.
5 It may be used under the terms of the GNU General Public License. */
9 #include "ffmpeg/avcodec.h"
11 struct hb_work_private_s
14 AVCodecContext * context;
18 int encavcodecInit( hb_work_object_t *, hb_job_t * );
19 int encavcodecWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
20 void encavcodecClose( hb_work_object_t * );
22 hb_work_object_t hb_encavcodec =
25 "MPEG-4 encoder (libavcodec)",
31 int encavcodecInit( hb_work_object_t * w, hb_job_t * job )
34 AVCodecContext * context;
36 hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
41 codec = avcodec_find_encoder( CODEC_ID_MPEG4 );
44 hb_log( "hb_work_encavcodec_init: avcodec_find_encoder "
47 context = avcodec_alloc_context();
48 if( job->vquality < 0.0 || job->vquality > 1.0 )
51 context->bit_rate = 1000 * job->vbitrate;
52 context->bit_rate_tolerance = 10 * context->bit_rate;
56 /* Constant quantizer */
57 context->qmin = 31 - job->vquality * 30;
58 context->qmax = context->qmin;
59 hb_log( "encavcodec: encoding at constant quantizer %d",
62 context->width = job->width;
63 context->height = job->height;
64 context->time_base = (AVRational) { job->vrate_base, job->vrate };
65 context->gop_size = 10 * job->vrate / job->vrate_base;
66 context->pix_fmt = PIX_FMT_YUV420P;
68 if( job->mux & HB_MUX_MP4 )
70 context->flags |= CODEC_FLAG_GLOBAL_HEADER;
74 context->flags |= CODEC_FLAG_GRAY;
79 char filename[1024]; memset( filename, 0, 1024 );
80 hb_get_tempory_filename( job->h, filename, "ffmpeg.log" );
84 pv->file = fopen( filename, "wb" );
85 context->flags |= CODEC_FLAG_PASS1;
92 pv->file = fopen( filename, "rb" );
93 fseek( pv->file, 0, SEEK_END );
94 size = ftell( pv->file );
95 fseek( pv->file, 0, SEEK_SET );
96 log = malloc( size + 1 );
98 fread( log, size, 1, pv->file );
102 context->flags |= CODEC_FLAG_PASS2;
103 context->stats_in = log;
107 if( avcodec_open( context, codec ) )
109 hb_log( "hb_work_encavcodec_init: avcodec_open failed" );
111 pv->context = context;
113 if( ( job->mux & HB_MUX_MP4 ) && job->pass != 1 )
116 w->config->mpeg4.length = 15;
117 memcpy( w->config->mpeg4.bytes, context->extradata + 15, 15 );
123 /***********************************************************************
125 ***********************************************************************
127 **********************************************************************/
128 void encavcodecClose( hb_work_object_t * w )
130 hb_work_private_t * pv = w->private_data;
134 hb_log( "encavcodec: closing libavcodec" );
135 avcodec_close( pv->context );
143 /***********************************************************************
145 ***********************************************************************
147 **********************************************************************/
148 int encavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
149 hb_buffer_t ** buf_out )
151 hb_work_private_t * pv = w->private_data;
152 hb_job_t * job = pv->job;
154 hb_buffer_t * in = *buf_in, * buf;
156 frame = avcodec_alloc_frame();
157 frame->data[0] = in->data;
158 frame->data[1] = frame->data[0] + job->width * job->height;
159 frame->data[2] = frame->data[1] + job->width * job->height / 4;
160 frame->linesize[0] = job->width;
161 frame->linesize[1] = job->width / 2;
162 frame->linesize[2] = job->width / 2;
164 /* Should be way too large */
165 buf = hb_buffer_init( 3 * job->width * job->height / 2 );
166 buf->size = avcodec_encode_video( pv->context, buf->data, buf->alloc,
168 buf->start = in->start;
169 buf->stop = in->stop;
170 buf->key = pv->context->coded_frame->key_frame;
177 fprintf( pv->file, "%s", pv->context->stats_out );