Mercurial > libavcodec.hg
comparison libdiracenc.c @ 10060:965220ebc611 libavcodec
cosmetics: indentation, prettyprinting, K&R coding style
author | diego |
---|---|
date | Sat, 15 Aug 2009 11:42:15 +0000 |
parents | c6ace59701ce |
children | 38cfe222e1a4 |
comparison
equal
deleted
inserted
replaced
10059:c6ace59701ce | 10060:965220ebc611 |
---|---|
36 | 36 |
37 | 37 |
38 #include <libdirac_encoder/dirac_encoder.h> | 38 #include <libdirac_encoder/dirac_encoder.h> |
39 | 39 |
40 /** Dirac encoder private data */ | 40 /** Dirac encoder private data */ |
41 typedef struct FfmpegDiracEncoderParams | 41 typedef struct FfmpegDiracEncoderParams { |
42 { | |
43 /** Dirac encoder context */ | 42 /** Dirac encoder context */ |
44 dirac_encoder_context_t enc_ctx; | 43 dirac_encoder_context_t enc_ctx; |
45 | 44 |
46 /** frame being encoded */ | 45 /** frame being encoded */ |
47 AVFrame picture; | 46 AVFrame picture; |
112 | 111 |
113 /** | 112 /** |
114 * Returns the video format preset matching the input video dimensions and | 113 * Returns the video format preset matching the input video dimensions and |
115 * time base. | 114 * time base. |
116 */ | 115 */ |
117 static VideoFormat GetDiracVideoFormatPreset (AVCodecContext *avccontext) | 116 static VideoFormat GetDiracVideoFormatPreset(AVCodecContext *avccontext) |
118 { | 117 { |
119 unsigned int num_formats = sizeof(ff_dirac_video_formats) / | 118 unsigned int num_formats = sizeof(ff_dirac_video_formats) / |
120 sizeof(ff_dirac_video_formats[0]); | 119 sizeof(ff_dirac_video_formats[0]); |
121 | 120 |
122 unsigned int idx = ff_dirac_schro_get_video_format_idx (avccontext); | 121 unsigned int idx = ff_dirac_schro_get_video_format_idx(avccontext); |
123 | 122 |
124 return (idx < num_formats) ? | 123 return (idx < num_formats) ? |
125 ff_dirac_video_formats[idx] : VIDEO_FORMAT_CUSTOM; | 124 ff_dirac_video_formats[idx] : VIDEO_FORMAT_CUSTOM; |
126 } | 125 } |
127 | 126 |
128 static av_cold int libdirac_encode_init(AVCodecContext *avccontext) | 127 static av_cold int libdirac_encode_init(AVCodecContext *avccontext) |
129 { | 128 { |
130 | 129 |
131 FfmpegDiracEncoderParams* p_dirac_params = avccontext->priv_data; | 130 FfmpegDiracEncoderParams* p_dirac_params = avccontext->priv_data; |
132 int no_local = 1; | 131 int no_local = 1; |
133 int verbose = avccontext->debug; | 132 int verbose = avccontext->debug; |
134 VideoFormat preset; | 133 VideoFormat preset; |
135 | 134 |
136 /* get Dirac preset */ | 135 /* get Dirac preset */ |
137 preset = GetDiracVideoFormatPreset(avccontext); | 136 preset = GetDiracVideoFormatPreset(avccontext); |
138 | 137 |
139 /* initialize the encoder context */ | 138 /* initialize the encoder context */ |
140 dirac_encoder_context_init (&(p_dirac_params->enc_ctx), preset); | 139 dirac_encoder_context_init(&(p_dirac_params->enc_ctx), preset); |
141 | 140 |
142 p_dirac_params->enc_ctx.src_params.chroma = | 141 p_dirac_params->enc_ctx.src_params.chroma = GetDiracChromaFormat(avccontext->pix_fmt); |
143 GetDiracChromaFormat(avccontext->pix_fmt); | |
144 | 142 |
145 if (p_dirac_params->enc_ctx.src_params.chroma == formatNK) { | 143 if (p_dirac_params->enc_ctx.src_params.chroma == formatNK) { |
146 av_log (avccontext, AV_LOG_ERROR, | 144 av_log(avccontext, AV_LOG_ERROR, |
147 "Unsupported pixel format %d. This codec supports only " | 145 "Unsupported pixel format %d. This codec supports only " |
148 "Planar YUV formats (yuv420p, yuv422p, yuv444p\n", | 146 "Planar YUV formats (yuv420p, yuv422p, yuv444p\n", |
149 avccontext->pix_fmt); | 147 avccontext->pix_fmt); |
150 return -1; | 148 return -1; |
151 } | 149 } |
152 | 150 |
153 p_dirac_params->enc_ctx.src_params.frame_rate.numerator = | 151 p_dirac_params->enc_ctx.src_params.frame_rate.numerator = avccontext->time_base.den; |
154 avccontext->time_base.den; | 152 p_dirac_params->enc_ctx.src_params.frame_rate.denominator = avccontext->time_base.num; |
155 p_dirac_params->enc_ctx.src_params.frame_rate.denominator = | |
156 avccontext->time_base.num; | |
157 | 153 |
158 p_dirac_params->enc_ctx.src_params.width = avccontext->width; | 154 p_dirac_params->enc_ctx.src_params.width = avccontext->width; |
159 p_dirac_params->enc_ctx.src_params.height = avccontext->height; | 155 p_dirac_params->enc_ctx.src_params.height = avccontext->height; |
160 | 156 |
161 p_dirac_params->frame_size = avpicture_get_size(avccontext->pix_fmt, | 157 p_dirac_params->frame_size = avpicture_get_size(avccontext->pix_fmt, |
180 } else | 176 } else |
181 avccontext->has_b_frames = 1; | 177 avccontext->has_b_frames = 1; |
182 | 178 |
183 if (avccontext->flags & CODEC_FLAG_QSCALE) { | 179 if (avccontext->flags & CODEC_FLAG_QSCALE) { |
184 if (avccontext->global_quality) { | 180 if (avccontext->global_quality) { |
185 p_dirac_params->enc_ctx.enc_params.qf = | 181 p_dirac_params->enc_ctx.enc_params.qf = avccontext->global_quality |
186 avccontext->global_quality / (FF_QP2LAMBDA*10.0); | 182 / (FF_QP2LAMBDA * 10.0); |
187 /* if it is not default bitrate then send target rate. */ | 183 /* if it is not default bitrate then send target rate. */ |
188 if (avccontext->bit_rate >= 1000 && | 184 if (avccontext->bit_rate >= 1000 && |
189 avccontext->bit_rate != 200000) | 185 avccontext->bit_rate != 200000) |
190 p_dirac_params->enc_ctx.enc_params.trate = | 186 p_dirac_params->enc_ctx.enc_params.trate = avccontext->bit_rate |
191 avccontext->bit_rate / 1000; | 187 / 1000; |
192 } else | 188 } else |
193 p_dirac_params->enc_ctx.enc_params.lossless = 1; | 189 p_dirac_params->enc_ctx.enc_params.lossless = 1; |
194 } else if (avccontext->bit_rate >= 1000) | 190 } else if (avccontext->bit_rate >= 1000) |
195 p_dirac_params->enc_ctx.enc_params.trate = avccontext->bit_rate / 1000; | 191 p_dirac_params->enc_ctx.enc_params.trate = avccontext->bit_rate / 1000; |
196 | 192 |
197 if ((preset > VIDEO_FORMAT_QCIF || preset < VIDEO_FORMAT_QSIF525) && | 193 if ((preset > VIDEO_FORMAT_QCIF || preset < VIDEO_FORMAT_QSIF525) && |
198 avccontext->bit_rate == 200000) | 194 avccontext->bit_rate == 200000) |
199 p_dirac_params->enc_ctx.enc_params.trate = 0; | 195 p_dirac_params->enc_ctx.enc_params.trate = 0; |
200 | 196 |
201 if (avccontext->flags & CODEC_FLAG_INTERLACED_ME) | 197 if (avccontext->flags & CODEC_FLAG_INTERLACED_ME) |
202 /* all material can be coded as interlaced or progressive | 198 /* all material can be coded as interlaced or progressive |
203 * irrespective of the type of source material */ | 199 * irrespective of the type of source material */ |
204 p_dirac_params->enc_ctx.enc_params.picture_coding_mode = 1; | 200 p_dirac_params->enc_ctx.enc_params.picture_coding_mode = 1; |
205 | 201 |
206 p_dirac_params->p_encoder = dirac_encoder_init (&(p_dirac_params->enc_ctx), | 202 p_dirac_params->p_encoder = dirac_encoder_init(&(p_dirac_params->enc_ctx), |
207 verbose ); | 203 verbose); |
208 | 204 |
209 if (!p_dirac_params->p_encoder) { | 205 if (!p_dirac_params->p_encoder) { |
210 av_log(avccontext, AV_LOG_ERROR, | 206 av_log(avccontext, AV_LOG_ERROR, |
211 "Unrecoverable Error: dirac_encoder_init failed. "); | 207 "Unrecoverable Error: dirac_encoder_init failed. "); |
212 return EXIT_FAILURE; | 208 return EXIT_FAILURE; |
216 p_dirac_params->p_in_frame_buf = av_malloc(p_dirac_params->frame_size); | 212 p_dirac_params->p_in_frame_buf = av_malloc(p_dirac_params->frame_size); |
217 | 213 |
218 /* initialize the encoded frame queue */ | 214 /* initialize the encoded frame queue */ |
219 ff_dirac_schro_queue_init(&p_dirac_params->enc_frame_queue); | 215 ff_dirac_schro_queue_init(&p_dirac_params->enc_frame_queue); |
220 | 216 |
221 return 0 ; | 217 return 0; |
222 } | 218 } |
223 | 219 |
224 static void DiracFreeFrame (void *data) | 220 static void DiracFreeFrame(void *data) |
225 { | 221 { |
226 FfmpegDiracSchroEncodedFrame *enc_frame = data; | 222 FfmpegDiracSchroEncodedFrame *enc_frame = data; |
227 | 223 |
228 av_freep (&(enc_frame->p_encbuf)); | 224 av_freep(&(enc_frame->p_encbuf)); |
229 av_free(enc_frame); | 225 av_free(enc_frame); |
230 } | 226 } |
231 | 227 |
232 static int libdirac_encode_frame(AVCodecContext *avccontext, | 228 static int libdirac_encode_frame(AVCodecContext *avccontext, |
233 unsigned char *frame, | 229 unsigned char *frame, |
234 int buf_size, void *data) | 230 int buf_size, void *data) |
235 { | 231 { |
236 int enc_size = 0; | 232 int enc_size = 0; |
237 dirac_encoder_state_t state; | 233 dirac_encoder_state_t state; |
238 FfmpegDiracEncoderParams* p_dirac_params = avccontext->priv_data; | 234 FfmpegDiracEncoderParams* p_dirac_params = avccontext->priv_data; |
239 FfmpegDiracSchroEncodedFrame* p_frame_output = NULL; | 235 FfmpegDiracSchroEncodedFrame* p_frame_output = NULL; |
240 FfmpegDiracSchroEncodedFrame* p_next_output_frame = NULL; | 236 FfmpegDiracSchroEncodedFrame* p_next_output_frame = NULL; |
241 int go = 1; | 237 int go = 1; |
242 int last_frame_in_sequence = 0; | 238 int last_frame_in_sequence = 0; |
243 | 239 |
244 if (!data) { | 240 if (!data) { |
245 /* push end of sequence if not already signalled */ | 241 /* push end of sequence if not already signalled */ |
246 if (!p_dirac_params->eos_signalled) { | 242 if (!p_dirac_params->eos_signalled) { |
247 dirac_encoder_end_sequence( p_dirac_params->p_encoder ); | 243 dirac_encoder_end_sequence(p_dirac_params->p_encoder); |
248 p_dirac_params->eos_signalled = 1; | 244 p_dirac_params->eos_signalled = 1; |
249 } | 245 } |
250 } else { | 246 } else { |
251 | 247 |
252 /* Allocate frame data to Dirac input buffer. | 248 /* Allocate frame data to Dirac input buffer. |
253 * Input line size may differ from what the codec supports, | 249 * Input line size may differ from what the codec supports, |
254 * especially when transcoding from one format to another. | 250 * especially when transcoding from one format to another. |
255 * So use avpicture_layout to copy the frame. */ | 251 * So use avpicture_layout to copy the frame. */ |
256 avpicture_layout ((AVPicture *)data, avccontext->pix_fmt, | 252 avpicture_layout((AVPicture *)data, avccontext->pix_fmt, |
257 avccontext->width, avccontext->height, | 253 avccontext->width, avccontext->height, |
258 p_dirac_params->p_in_frame_buf, | 254 p_dirac_params->p_in_frame_buf, |
259 p_dirac_params->frame_size); | 255 p_dirac_params->frame_size); |
260 | 256 |
261 /* load next frame */ | 257 /* load next frame */ |
262 if (dirac_encoder_load (p_dirac_params->p_encoder, | 258 if (dirac_encoder_load(p_dirac_params->p_encoder, |
263 p_dirac_params->p_in_frame_buf, | 259 p_dirac_params->p_in_frame_buf, |
264 p_dirac_params->frame_size ) < 0) { | 260 p_dirac_params->frame_size) < 0) { |
265 av_log(avccontext, AV_LOG_ERROR, "Unrecoverable Encoder Error." | 261 av_log(avccontext, AV_LOG_ERROR, "Unrecoverable Encoder Error." |
266 " dirac_encoder_load failed...\n"); | 262 " dirac_encoder_load failed...\n"); |
267 return -1; | 263 return -1; |
268 } | 264 } |
269 } | 265 } |
270 | 266 |
271 if (p_dirac_params->eos_pulled) | 267 if (p_dirac_params->eos_pulled) |
272 go = 0; | 268 go = 0; |
273 | 269 |
274 while(go) { | 270 while (go) { |
275 p_dirac_params->p_encoder->enc_buf.buffer = frame; | 271 p_dirac_params->p_encoder->enc_buf.buffer = frame; |
276 p_dirac_params->p_encoder->enc_buf.size = buf_size; | 272 p_dirac_params->p_encoder->enc_buf.size = buf_size; |
277 /* process frame */ | 273 /* process frame */ |
278 state = dirac_encoder_output ( p_dirac_params->p_encoder ); | 274 state = dirac_encoder_output(p_dirac_params->p_encoder); |
279 | 275 |
280 switch (state) | 276 switch (state) { |
281 { | |
282 case ENC_STATE_AVAIL: | 277 case ENC_STATE_AVAIL: |
283 case ENC_STATE_EOS: | 278 case ENC_STATE_EOS: |
284 assert (p_dirac_params->p_encoder->enc_buf.size > 0); | 279 assert(p_dirac_params->p_encoder->enc_buf.size > 0); |
285 | 280 |
286 /* All non-frame data is prepended to actual frame data to | 281 /* All non-frame data is prepended to actual frame data to |
287 * be able to set the pts correctly. So we don't write data | 282 * be able to set the pts correctly. So we don't write data |
288 * to the frame output queue until we actually have a frame | 283 * to the frame output queue until we actually have a frame |
289 */ | 284 */ |
290 | 285 |
291 p_dirac_params->enc_buf = av_realloc ( | 286 p_dirac_params->enc_buf = av_realloc(p_dirac_params->enc_buf, |
292 p_dirac_params->enc_buf, | 287 p_dirac_params->enc_buf_size + |
293 p_dirac_params->enc_buf_size + | 288 p_dirac_params->p_encoder->enc_buf.size); |
294 p_dirac_params->p_encoder->enc_buf.size | |
295 ); | |
296 memcpy(p_dirac_params->enc_buf + p_dirac_params->enc_buf_size, | 289 memcpy(p_dirac_params->enc_buf + p_dirac_params->enc_buf_size, |
297 p_dirac_params->p_encoder->enc_buf.buffer, | 290 p_dirac_params->p_encoder->enc_buf.buffer, |
298 p_dirac_params->p_encoder->enc_buf.size); | 291 p_dirac_params->p_encoder->enc_buf.size); |
299 | 292 |
300 p_dirac_params->enc_buf_size += | 293 p_dirac_params->enc_buf_size += p_dirac_params->p_encoder->enc_buf.size; |
301 p_dirac_params->p_encoder->enc_buf.size; | |
302 | 294 |
303 if (state == ENC_STATE_EOS) { | 295 if (state == ENC_STATE_EOS) { |
304 p_dirac_params->eos_pulled = 1; | 296 p_dirac_params->eos_pulled = 1; |
305 go = 0; | 297 go = 0; |
306 } | 298 } |
311 break; | 303 break; |
312 | 304 |
313 /* create output frame */ | 305 /* create output frame */ |
314 p_frame_output = av_mallocz(sizeof(FfmpegDiracSchroEncodedFrame)); | 306 p_frame_output = av_mallocz(sizeof(FfmpegDiracSchroEncodedFrame)); |
315 /* set output data */ | 307 /* set output data */ |
316 p_frame_output->size = p_dirac_params->enc_buf_size; | 308 p_frame_output->size = p_dirac_params->enc_buf_size; |
317 p_frame_output->p_encbuf = p_dirac_params->enc_buf; | 309 p_frame_output->p_encbuf = p_dirac_params->enc_buf; |
318 p_frame_output->frame_num = | 310 p_frame_output->frame_num = p_dirac_params->p_encoder->enc_pparams.pnum; |
319 p_dirac_params->p_encoder->enc_pparams.pnum; | |
320 | 311 |
321 if (p_dirac_params->p_encoder->enc_pparams.ptype == INTRA_PICTURE && | 312 if (p_dirac_params->p_encoder->enc_pparams.ptype == INTRA_PICTURE && |
322 p_dirac_params->p_encoder->enc_pparams.rtype == REFERENCE_PICTURE) | 313 p_dirac_params->p_encoder->enc_pparams.rtype == REFERENCE_PICTURE) |
323 p_frame_output->key_frame = 1; | 314 p_frame_output->key_frame = 1; |
324 | 315 |
325 ff_dirac_schro_queue_push_back (&p_dirac_params->enc_frame_queue, | 316 ff_dirac_schro_queue_push_back(&p_dirac_params->enc_frame_queue, |
326 p_frame_output); | 317 p_frame_output); |
327 | 318 |
328 p_dirac_params->enc_buf_size = 0; | 319 p_dirac_params->enc_buf_size = 0; |
329 p_dirac_params->enc_buf = NULL; | 320 p_dirac_params->enc_buf = NULL; |
330 break; | 321 break; |
331 | 322 |
344 } | 335 } |
345 } | 336 } |
346 | 337 |
347 /* copy 'next' frame in queue */ | 338 /* copy 'next' frame in queue */ |
348 | 339 |
349 if (p_dirac_params->enc_frame_queue.size == 1 && | 340 if (p_dirac_params->enc_frame_queue.size == 1 && p_dirac_params->eos_pulled) |
350 p_dirac_params->eos_pulled) | |
351 last_frame_in_sequence = 1; | 341 last_frame_in_sequence = 1; |
352 | 342 |
353 p_next_output_frame = | 343 p_next_output_frame = ff_dirac_schro_queue_pop(&p_dirac_params->enc_frame_queue); |
354 ff_dirac_schro_queue_pop(&p_dirac_params->enc_frame_queue); | |
355 | 344 |
356 if (!p_next_output_frame) | 345 if (!p_next_output_frame) |
357 return 0; | 346 return 0; |
358 | 347 |
359 memcpy(frame, p_next_output_frame->p_encbuf, p_next_output_frame->size); | 348 memcpy(frame, p_next_output_frame->p_encbuf, p_next_output_frame->size); |
364 avccontext->coded_frame->pts = p_next_output_frame->frame_num; | 353 avccontext->coded_frame->pts = p_next_output_frame->frame_num; |
365 enc_size = p_next_output_frame->size; | 354 enc_size = p_next_output_frame->size; |
366 | 355 |
367 /* Append the end of sequence information to the last frame in the | 356 /* Append the end of sequence information to the last frame in the |
368 * sequence. */ | 357 * sequence. */ |
369 if (last_frame_in_sequence && p_dirac_params->enc_buf_size > 0) | 358 if (last_frame_in_sequence && p_dirac_params->enc_buf_size > 0) { |
370 { | 359 memcpy(frame + enc_size, p_dirac_params->enc_buf, |
371 memcpy (frame + enc_size, p_dirac_params->enc_buf, | 360 p_dirac_params->enc_buf_size); |
372 p_dirac_params->enc_buf_size); | |
373 enc_size += p_dirac_params->enc_buf_size; | 361 enc_size += p_dirac_params->enc_buf_size; |
374 av_freep (&p_dirac_params->enc_buf); | 362 av_freep(&p_dirac_params->enc_buf); |
375 p_dirac_params->enc_buf_size = 0; | 363 p_dirac_params->enc_buf_size = 0; |
376 } | 364 } |
377 | 365 |
378 /* free frame */ | 366 /* free frame */ |
379 DiracFreeFrame(p_next_output_frame); | 367 DiracFreeFrame(p_next_output_frame); |
384 static av_cold int libdirac_encode_close(AVCodecContext *avccontext) | 372 static av_cold int libdirac_encode_close(AVCodecContext *avccontext) |
385 { | 373 { |
386 FfmpegDiracEncoderParams* p_dirac_params = avccontext->priv_data; | 374 FfmpegDiracEncoderParams* p_dirac_params = avccontext->priv_data; |
387 | 375 |
388 /* close the encoder */ | 376 /* close the encoder */ |
389 dirac_encoder_close(p_dirac_params->p_encoder ); | 377 dirac_encoder_close(p_dirac_params->p_encoder); |
390 | 378 |
391 /* free data in the output frame queue */ | 379 /* free data in the output frame queue */ |
392 ff_dirac_schro_queue_free(&p_dirac_params->enc_frame_queue, | 380 ff_dirac_schro_queue_free(&p_dirac_params->enc_frame_queue, |
393 DiracFreeFrame); | 381 DiracFreeFrame); |
394 | 382 |
397 av_freep(&p_dirac_params->enc_buf); | 385 av_freep(&p_dirac_params->enc_buf); |
398 | 386 |
399 /* free the input frame buffer */ | 387 /* free the input frame buffer */ |
400 av_freep(&p_dirac_params->p_in_frame_buf); | 388 av_freep(&p_dirac_params->p_in_frame_buf); |
401 | 389 |
402 return 0 ; | 390 return 0; |
403 } | 391 } |
404 | 392 |
405 | 393 |
406 AVCodec libdirac_encoder = { | 394 AVCodec libdirac_encoder = { |
407 "libdirac", | 395 "libdirac", |
409 CODEC_ID_DIRAC, | 397 CODEC_ID_DIRAC, |
410 sizeof(FfmpegDiracEncoderParams), | 398 sizeof(FfmpegDiracEncoderParams), |
411 libdirac_encode_init, | 399 libdirac_encode_init, |
412 libdirac_encode_frame, | 400 libdirac_encode_frame, |
413 libdirac_encode_close, | 401 libdirac_encode_close, |
414 .capabilities= CODEC_CAP_DELAY, | 402 .capabilities = CODEC_CAP_DELAY, |
415 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_NONE}, | 403 .pix_fmts = (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_NONE}, |
416 .long_name= NULL_IF_CONFIG_SMALL("libdirac Dirac 2.2"), | 404 .long_name = NULL_IF_CONFIG_SMALL("libdirac Dirac 2.2"), |
417 } ; | 405 }; |