OSDN Git Service

f0460690698d022ef352f023ba0faa012279efc7
[handbrake-jp/handbrake-jp-git.git] / libhb / encavcodec.c
1 /* $Id: encavcodec.c,v 1.23 2005/10/13 23:47:06 titer Exp $
2
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. */
6
7 #include "hb.h"
8
9 #include "ffmpeg/avcodec.h"
10
11 struct hb_work_private_s
12 {
13     hb_job_t * job;
14     AVCodecContext * context;
15     FILE * file;
16 };
17
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 * );
21
22 hb_work_object_t hb_encavcodec =
23 {   
24     WORK_ENCAVCODEC,
25     "MPEG-4 encoder (libavcodec)",
26     encavcodecInit,
27     encavcodecWork,
28     encavcodecClose
29 }; 
30
31 int encavcodecInit( hb_work_object_t * w, hb_job_t * job )
32 {
33     AVCodec * codec;
34     AVCodecContext * context;
35     
36     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
37     w->private_data = pv;
38
39     pv->job = job;
40
41     codec = avcodec_find_encoder( CODEC_ID_MPEG4 );
42     if( !codec )
43     {
44         hb_log( "hb_work_encavcodec_init: avcodec_find_encoder "
45                 "failed" );
46     }
47     context = avcodec_alloc_context();
48     if( job->vquality < 0.0 || job->vquality > 1.0 )
49     {
50         /* Rate control */
51         context->bit_rate = 1000 * job->vbitrate;
52         context->bit_rate_tolerance = 10 * context->bit_rate;
53     }
54     else
55     {
56         /* Constant quantizer */
57         context->qmin = 31 - job->vquality * 30;
58         context->qmax = context->qmin;
59         hb_log( "encavcodec: encoding at constant quantizer %d",
60                 context->qmin );
61     }
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;
67
68     if( job->mux & ( HB_MUX_MP4 | HB_MUX_PSP ) )
69     {
70         context->flags |= CODEC_FLAG_GLOBAL_HEADER;
71     }
72     if( job->mux & HB_MUX_PSP )
73     {
74         context->flags |= CODEC_FLAG_BITEXACT;
75     }
76     if( job->grayscale )
77     {
78         context->flags |= CODEC_FLAG_GRAY;
79     }
80
81     if( job->pass )
82     {
83         char filename[1024]; memset( filename, 0, 1024 );
84         hb_get_tempory_filename( job->h, filename, "ffmpeg.log" );
85
86         if( job->pass == 1 )
87         {
88             pv->file = fopen( filename, "wb" );
89             context->flags |= CODEC_FLAG_PASS1;
90         }
91         else
92         {
93             int    size;
94             char * log;
95
96             pv->file = fopen( filename, "rb" );
97             fseek( pv->file, 0, SEEK_END );
98             size = ftell( pv->file );
99             fseek( pv->file, 0, SEEK_SET );
100             log = malloc( size + 1 );
101             log[size] = '\0';
102             fread( log, size, 1, pv->file );
103             fclose( pv->file );
104             pv->file = NULL;
105
106             context->flags    |= CODEC_FLAG_PASS2;
107             context->stats_in  = log;
108         }
109     }
110
111     if( avcodec_open( context, codec ) )
112     {
113         hb_log( "hb_work_encavcodec_init: avcodec_open failed" );
114     }
115     pv->context = context;
116
117     if( ( job->mux & ( HB_MUX_MP4 | HB_MUX_PSP ) ) && job->pass != 1 )
118     {
119 #if 0
120         /* Hem hem */
121         w->config->mpeg4.length = 15;
122         memcpy( w->config->mpeg4.bytes, context->extradata + 15, 15 );
123 #else
124         w->config->mpeg4.length = context->extradata_size;
125         memcpy( w->config->mpeg4.bytes, context->extradata,
126                 context->extradata_size );
127 #endif
128     }
129     
130     return 0;
131 }
132
133 /***********************************************************************
134  * Close
135  ***********************************************************************
136  *
137  **********************************************************************/
138 void encavcodecClose( hb_work_object_t * w )
139 {
140     hb_work_private_t * pv = w->private_data;
141
142     if( pv->context )
143     {
144         hb_log( "encavcodec: closing libavcodec" );
145         avcodec_close( pv->context );
146     }
147     if( pv->file )
148     {
149         fclose( pv->file );
150     }
151 }
152
153 /***********************************************************************
154  * Work
155  ***********************************************************************
156  *
157  **********************************************************************/
158 int encavcodecWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
159                     hb_buffer_t ** buf_out )
160 {
161     hb_work_private_t * pv = w->private_data;
162     hb_job_t * job = pv->job;
163     AVFrame  * frame;
164     hb_buffer_t * in = *buf_in, * buf;
165
166     frame              = avcodec_alloc_frame();
167     frame->data[0]     = in->data;
168     frame->data[1]     = frame->data[0] + job->width * job->height;
169     frame->data[2]     = frame->data[1] + job->width * job->height / 4;
170     frame->linesize[0] = job->width;
171     frame->linesize[1] = job->width / 2;
172     frame->linesize[2] = job->width / 2;
173
174     /* Should be way too large */
175     buf = hb_buffer_init( 3 * job->width * job->height / 2 );
176     buf->size = avcodec_encode_video( pv->context, buf->data, buf->alloc,
177                                       frame );
178     buf->start = in->start;
179     buf->stop  = in->stop;
180     buf->key   = pv->context->coded_frame->key_frame;
181
182     av_free( frame );
183
184     if( job->pass == 1 )
185     {
186         /* Write stats */
187         fprintf( pv->file, "%s", pv->context->stats_out );
188     }
189
190     *buf_out = buf;
191
192     return HB_WORK_OK;
193 }
194
195