Mercurial > libavcodec.hg
annotate libdiracenc.c @ 10059:c6ace59701ce libavcodec
Drop some more useless braces around if/for constructs.
author | diego |
---|---|
date | Sat, 15 Aug 2009 11:32:11 +0000 |
parents | 646065f63290 |
children | 965220ebc611 |
rev | line source |
---|---|
6734 | 1 /* |
2 * Dirac encoding support via libdirac library | |
3 * Copyright (c) 2005 BBC, Andrew Kennedy <dirac at rd dot bbc dot co dot uk> | |
4 * Copyright (c) 2006-2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com > | |
5 * | |
6 * This file is part of FFmpeg. | |
7 * | |
8 * FFmpeg is free software; you can redistribute it and/or | |
9 * modify it under the terms of the GNU Lesser General Public | |
10 * License as published by the Free Software Foundation; either | |
11 * version 2.1 of the License, or (at your option) any later version. | |
12 * | |
13 * FFmpeg is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Lesser General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Lesser General Public | |
19 * License along with FFmpeg; if not, write to the Free Software | |
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 */ | |
22 | |
23 /** | |
8718
e9d9d946f213
Use full internal pathname in doxygen @file directives.
diego
parents:
8021
diff
changeset
|
24 * @file libavcodec/libdiracenc.c |
6734 | 25 * Dirac encoding support via libdirac library; more details about the |
26 * Dirac project can be found at http://dirac.sourceforge.net/. | |
27 * The libdirac_encoder library implements Dirac specification version 2.2 | |
28 * (http://dirac.sourceforge.net/specification.html). | |
29 */ | |
30 | |
31 #include "libdirac_libschro.h" | |
32 #include "libdirac.h" | |
33 | |
34 #undef NDEBUG | |
35 #include <assert.h> | |
36 | |
37 | |
38 #include <libdirac_encoder/dirac_encoder.h> | |
39 | |
40 /** Dirac encoder private data */ | |
41 typedef struct FfmpegDiracEncoderParams | |
42 { | |
43 /** Dirac encoder context */ | |
44 dirac_encoder_context_t enc_ctx; | |
45 | |
46 /** frame being encoded */ | |
47 AVFrame picture; | |
48 | |
49 /** frame size */ | |
50 int frame_size; | |
51 | |
52 /** Dirac encoder handle */ | |
53 dirac_encoder_t* p_encoder; | |
54 | |
55 /** input frame buffer */ | |
56 unsigned char *p_in_frame_buf; | |
57 | |
7252 | 58 /** buffer to store encoder output before writing it to the frame queue */ |
59 unsigned char *enc_buf; | |
60 | |
61 /** size of encoder buffer */ | |
62 int enc_buf_size; | |
63 | |
6734 | 64 /** queue storing encoded frames */ |
65 FfmpegDiracSchroQueue enc_frame_queue; | |
66 | |
67 /** end of sequence signalled by user, 0 - false, 1 - true */ | |
68 int eos_signalled; | |
69 | |
70 /** end of sequence returned by encoder, 0 - false, 1 - true */ | |
71 int eos_pulled; | |
72 } FfmpegDiracEncoderParams; | |
73 | |
74 /** | |
75 * Works out Dirac-compatible chroma format. | |
76 */ | |
77 static dirac_chroma_t GetDiracChromaFormat(enum PixelFormat ff_pix_fmt) | |
78 { | |
79 int num_formats = sizeof(ffmpeg_dirac_pixel_format_map) / | |
80 sizeof(ffmpeg_dirac_pixel_format_map[0]); | |
81 int idx; | |
82 | |
10059
c6ace59701ce
Drop some more useless braces around if/for constructs.
diego
parents:
10056
diff
changeset
|
83 for (idx = 0; idx < num_formats; ++idx) |
c6ace59701ce
Drop some more useless braces around if/for constructs.
diego
parents:
10056
diff
changeset
|
84 if (ffmpeg_dirac_pixel_format_map[idx].ff_pix_fmt == ff_pix_fmt) |
6734 | 85 return ffmpeg_dirac_pixel_format_map[idx].dirac_pix_fmt; |
86 return formatNK; | |
87 } | |
88 | |
89 /** | |
90 * Dirac video preset table. Ensure that this tables matches up correctly | |
91 * with the ff_dirac_schro_video_format_info table in libdirac_libschro.c. | |
92 */ | |
93 static const VideoFormat ff_dirac_video_formats[]={ | |
94 VIDEO_FORMAT_CUSTOM , | |
95 VIDEO_FORMAT_QSIF525 , | |
96 VIDEO_FORMAT_QCIF , | |
97 VIDEO_FORMAT_SIF525 , | |
98 VIDEO_FORMAT_CIF , | |
99 VIDEO_FORMAT_4SIF525 , | |
100 VIDEO_FORMAT_4CIF , | |
101 VIDEO_FORMAT_SD_480I60 , | |
102 VIDEO_FORMAT_SD_576I50 , | |
103 VIDEO_FORMAT_HD_720P60 , | |
104 VIDEO_FORMAT_HD_720P50 , | |
105 VIDEO_FORMAT_HD_1080I60 , | |
106 VIDEO_FORMAT_HD_1080I50 , | |
107 VIDEO_FORMAT_HD_1080P60 , | |
108 VIDEO_FORMAT_HD_1080P50 , | |
109 VIDEO_FORMAT_DIGI_CINEMA_2K24 , | |
110 VIDEO_FORMAT_DIGI_CINEMA_4K24 , | |
111 }; | |
112 | |
113 /** | |
114 * Returns the video format preset matching the input video dimensions and | |
115 * time base. | |
116 */ | |
117 static VideoFormat GetDiracVideoFormatPreset (AVCodecContext *avccontext) | |
118 { | |
119 unsigned int num_formats = sizeof(ff_dirac_video_formats) / | |
120 sizeof(ff_dirac_video_formats[0]); | |
121 | |
122 unsigned int idx = ff_dirac_schro_get_video_format_idx (avccontext); | |
123 | |
124 return (idx < num_formats) ? | |
125 ff_dirac_video_formats[idx] : VIDEO_FORMAT_CUSTOM; | |
126 } | |
127 | |
9007
043574c5c153
Add missing av_cold in static init/close functions.
stefano
parents:
8718
diff
changeset
|
128 static av_cold int libdirac_encode_init(AVCodecContext *avccontext) |
6734 | 129 { |
130 | |
131 FfmpegDiracEncoderParams* p_dirac_params = avccontext->priv_data; | |
132 int no_local = 1; | |
133 int verbose = avccontext->debug; | |
134 VideoFormat preset; | |
135 | |
136 /* get Dirac preset */ | |
137 preset = GetDiracVideoFormatPreset(avccontext); | |
138 | |
139 /* initialize the encoder context */ | |
140 dirac_encoder_context_init (&(p_dirac_params->enc_ctx), preset); | |
141 | |
142 p_dirac_params->enc_ctx.src_params.chroma = | |
143 GetDiracChromaFormat(avccontext->pix_fmt); | |
144 | |
145 if (p_dirac_params->enc_ctx.src_params.chroma == formatNK) { | |
146 av_log (avccontext, AV_LOG_ERROR, | |
147 "Unsupported pixel format %d. This codec supports only " | |
148 "Planar YUV formats (yuv420p, yuv422p, yuv444p\n", | |
149 avccontext->pix_fmt); | |
150 return -1; | |
151 } | |
152 | |
153 p_dirac_params->enc_ctx.src_params.frame_rate.numerator = | |
154 avccontext->time_base.den; | |
155 p_dirac_params->enc_ctx.src_params.frame_rate.denominator = | |
156 avccontext->time_base.num; | |
157 | |
158 p_dirac_params->enc_ctx.src_params.width = avccontext->width; | |
159 p_dirac_params->enc_ctx.src_params.height = avccontext->height; | |
160 | |
161 p_dirac_params->frame_size = avpicture_get_size(avccontext->pix_fmt, | |
162 avccontext->width, | |
163 avccontext->height); | |
164 | |
165 avccontext->coded_frame = &p_dirac_params->picture; | |
166 | |
167 if (no_local) { | |
168 p_dirac_params->enc_ctx.decode_flag = 0; | |
169 p_dirac_params->enc_ctx.instr_flag = 0; | |
170 } else { | |
171 p_dirac_params->enc_ctx.decode_flag = 1; | |
172 p_dirac_params->enc_ctx.instr_flag = 1; | |
173 } | |
174 | |
175 /* Intra-only sequence */ | |
10055 | 176 if (!avccontext->gop_size) { |
6734 | 177 p_dirac_params->enc_ctx.enc_params.num_L1 = 0; |
7839
e6348a5656e0
Add support for creating Simple Profile (I-frame only, no arithmetic coding)
diego
parents:
7252
diff
changeset
|
178 if (avccontext->coder_type == FF_CODER_TYPE_VLC) |
e6348a5656e0
Add support for creating Simple Profile (I-frame only, no arithmetic coding)
diego
parents:
7252
diff
changeset
|
179 p_dirac_params->enc_ctx.enc_params.using_ac = 0; |
e6348a5656e0
Add support for creating Simple Profile (I-frame only, no arithmetic coding)
diego
parents:
7252
diff
changeset
|
180 } else |
6734 | 181 avccontext->has_b_frames = 1; |
182 | |
183 if (avccontext->flags & CODEC_FLAG_QSCALE) { | |
10055 | 184 if (avccontext->global_quality) { |
6734 | 185 p_dirac_params->enc_ctx.enc_params.qf = |
186 avccontext->global_quality / (FF_QP2LAMBDA*10.0); | |
187 /* if it is not default bitrate then send target rate. */ | |
188 if (avccontext->bit_rate >= 1000 && | |
10056
646065f63290
Remove useless braces around if/for/while expressions.
diego
parents:
10055
diff
changeset
|
189 avccontext->bit_rate != 200000) |
6734 | 190 p_dirac_params->enc_ctx.enc_params.trate = |
191 avccontext->bit_rate / 1000; | |
192 } else | |
193 p_dirac_params->enc_ctx.enc_params.lossless = 1; | |
194 } else if (avccontext->bit_rate >= 1000) | |
195 p_dirac_params->enc_ctx.enc_params.trate = avccontext->bit_rate / 1000; | |
196 | |
197 if ((preset > VIDEO_FORMAT_QCIF || preset < VIDEO_FORMAT_QSIF525) && | |
10056
646065f63290
Remove useless braces around if/for/while expressions.
diego
parents:
10055
diff
changeset
|
198 avccontext->bit_rate == 200000) |
6734 | 199 p_dirac_params->enc_ctx.enc_params.trate = 0; |
200 | |
10056
646065f63290
Remove useless braces around if/for/while expressions.
diego
parents:
10055
diff
changeset
|
201 if (avccontext->flags & CODEC_FLAG_INTERLACED_ME) |
6734 | 202 /* all material can be coded as interlaced or progressive |
203 * irrespective of the type of source material */ | |
204 p_dirac_params->enc_ctx.enc_params.picture_coding_mode = 1; | |
205 | |
206 p_dirac_params->p_encoder = dirac_encoder_init (&(p_dirac_params->enc_ctx), | |
207 verbose ); | |
208 | |
209 if (!p_dirac_params->p_encoder) { | |
210 av_log(avccontext, AV_LOG_ERROR, | |
211 "Unrecoverable Error: dirac_encoder_init failed. "); | |
212 return EXIT_FAILURE; | |
213 } | |
214 | |
215 /* allocate enough memory for the incoming data */ | |
216 p_dirac_params->p_in_frame_buf = av_malloc(p_dirac_params->frame_size); | |
217 | |
218 /* initialize the encoded frame queue */ | |
219 ff_dirac_schro_queue_init(&p_dirac_params->enc_frame_queue); | |
220 | |
221 return 0 ; | |
222 } | |
223 | |
224 static void DiracFreeFrame (void *data) | |
225 { | |
226 FfmpegDiracSchroEncodedFrame *enc_frame = data; | |
227 | |
228 av_freep (&(enc_frame->p_encbuf)); | |
229 av_free(enc_frame); | |
230 } | |
231 | |
232 static int libdirac_encode_frame(AVCodecContext *avccontext, | |
233 unsigned char *frame, | |
234 int buf_size, void *data) | |
235 { | |
236 int enc_size = 0; | |
237 dirac_encoder_state_t state; | |
238 FfmpegDiracEncoderParams* p_dirac_params = avccontext->priv_data; | |
239 FfmpegDiracSchroEncodedFrame* p_frame_output = NULL; | |
240 FfmpegDiracSchroEncodedFrame* p_next_output_frame = NULL; | |
241 int go = 1; | |
7252 | 242 int last_frame_in_sequence = 0; |
6734 | 243 |
10055 | 244 if (!data) { |
6734 | 245 /* push end of sequence if not already signalled */ |
246 if (!p_dirac_params->eos_signalled) { | |
247 dirac_encoder_end_sequence( p_dirac_params->p_encoder ); | |
248 p_dirac_params->eos_signalled = 1; | |
249 } | |
250 } else { | |
251 | |
252 /* Allocate frame data to Dirac input buffer. | |
253 * Input line size may differ from what the codec supports, | |
254 * especially when transcoding from one format to another. | |
255 * So use avpicture_layout to copy the frame. */ | |
256 avpicture_layout ((AVPicture *)data, avccontext->pix_fmt, | |
257 avccontext->width, avccontext->height, | |
258 p_dirac_params->p_in_frame_buf, | |
259 p_dirac_params->frame_size); | |
260 | |
261 /* load next frame */ | |
262 if (dirac_encoder_load (p_dirac_params->p_encoder, | |
263 p_dirac_params->p_in_frame_buf, | |
264 p_dirac_params->frame_size ) < 0) { | |
265 av_log(avccontext, AV_LOG_ERROR, "Unrecoverable Encoder Error." | |
266 " dirac_encoder_load failed...\n"); | |
267 return -1; | |
268 } | |
269 } | |
270 | |
271 if (p_dirac_params->eos_pulled) | |
272 go = 0; | |
273 | |
274 while(go) { | |
275 p_dirac_params->p_encoder->enc_buf.buffer = frame; | |
276 p_dirac_params->p_encoder->enc_buf.size = buf_size; | |
277 /* process frame */ | |
278 state = dirac_encoder_output ( p_dirac_params->p_encoder ); | |
279 | |
280 switch (state) | |
281 { | |
282 case ENC_STATE_AVAIL: | |
283 case ENC_STATE_EOS: | |
284 assert (p_dirac_params->p_encoder->enc_buf.size > 0); | |
7252 | 285 |
286 /* 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 | |
288 * to the frame output queue until we actually have a frame | |
289 */ | |
290 | |
291 p_dirac_params->enc_buf = av_realloc ( | |
292 p_dirac_params->enc_buf, | |
293 p_dirac_params->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, | |
297 p_dirac_params->p_encoder->enc_buf.buffer, | |
298 p_dirac_params->p_encoder->enc_buf.size); | |
299 | |
300 p_dirac_params->enc_buf_size += | |
301 p_dirac_params->p_encoder->enc_buf.size; | |
302 | |
303 if (state == ENC_STATE_EOS) { | |
304 p_dirac_params->eos_pulled = 1; | |
305 go = 0; | |
306 } | |
307 | |
308 /* If non-frame data, don't output it until it we get an | |
309 * encoded frame back from the encoder. */ | |
310 if (p_dirac_params->p_encoder->enc_pparams.pnum == -1) | |
311 break; | |
312 | |
6734 | 313 /* create output frame */ |
314 p_frame_output = av_mallocz(sizeof(FfmpegDiracSchroEncodedFrame)); | |
315 /* set output data */ | |
7252 | 316 p_frame_output->size = p_dirac_params->enc_buf_size; |
317 p_frame_output->p_encbuf = p_dirac_params->enc_buf; | |
6734 | 318 p_frame_output->frame_num = |
319 p_dirac_params->p_encoder->enc_pparams.pnum; | |
320 | |
321 if (p_dirac_params->p_encoder->enc_pparams.ptype == INTRA_PICTURE && | |
322 p_dirac_params->p_encoder->enc_pparams.rtype == REFERENCE_PICTURE) | |
323 p_frame_output->key_frame = 1; | |
324 | |
325 ff_dirac_schro_queue_push_back (&p_dirac_params->enc_frame_queue, | |
326 p_frame_output); | |
327 | |
7252 | 328 p_dirac_params->enc_buf_size = 0; |
329 p_dirac_params->enc_buf = NULL; | |
6734 | 330 break; |
331 | |
332 case ENC_STATE_BUFFER: | |
333 go = 0; | |
334 break; | |
335 | |
336 case ENC_STATE_INVALID: | |
337 av_log(avccontext, AV_LOG_ERROR, | |
338 "Unrecoverable Dirac Encoder Error. Quitting...\n"); | |
339 return -1; | |
340 | |
341 default: | |
342 av_log(avccontext, AV_LOG_ERROR, "Unknown Dirac Encoder state\n"); | |
343 return -1; | |
344 } | |
345 } | |
346 | |
347 /* copy 'next' frame in queue */ | |
7252 | 348 |
349 if (p_dirac_params->enc_frame_queue.size == 1 && | |
350 p_dirac_params->eos_pulled) | |
351 last_frame_in_sequence = 1; | |
352 | |
6734 | 353 p_next_output_frame = |
354 ff_dirac_schro_queue_pop(&p_dirac_params->enc_frame_queue); | |
355 | |
10055 | 356 if (!p_next_output_frame) |
6734 | 357 return 0; |
358 | |
359 memcpy(frame, p_next_output_frame->p_encbuf, p_next_output_frame->size); | |
360 avccontext->coded_frame->key_frame = p_next_output_frame->key_frame; | |
361 /* Use the frame number of the encoded frame as the pts. It is OK to do | |
362 * so since Dirac is a constant framerate codec. It expects input to be | |
363 * of constant framerate. */ | |
364 avccontext->coded_frame->pts = p_next_output_frame->frame_num; | |
365 enc_size = p_next_output_frame->size; | |
366 | |
7252 | 367 /* Append the end of sequence information to the last frame in the |
368 * sequence. */ | |
369 if (last_frame_in_sequence && p_dirac_params->enc_buf_size > 0) | |
370 { | |
371 memcpy (frame + enc_size, p_dirac_params->enc_buf, | |
372 p_dirac_params->enc_buf_size); | |
373 enc_size += p_dirac_params->enc_buf_size; | |
374 av_freep (&p_dirac_params->enc_buf); | |
375 p_dirac_params->enc_buf_size = 0; | |
376 } | |
377 | |
6734 | 378 /* free frame */ |
379 DiracFreeFrame(p_next_output_frame); | |
380 | |
381 return enc_size; | |
382 } | |
383 | |
9007
043574c5c153
Add missing av_cold in static init/close functions.
stefano
parents:
8718
diff
changeset
|
384 static av_cold int libdirac_encode_close(AVCodecContext *avccontext) |
6734 | 385 { |
386 FfmpegDiracEncoderParams* p_dirac_params = avccontext->priv_data; | |
387 | |
388 /* close the encoder */ | |
389 dirac_encoder_close(p_dirac_params->p_encoder ); | |
390 | |
391 /* free data in the output frame queue */ | |
392 ff_dirac_schro_queue_free(&p_dirac_params->enc_frame_queue, | |
393 DiracFreeFrame); | |
394 | |
7252 | 395 /* free the encoder buffer */ |
396 if (p_dirac_params->enc_buf_size) | |
397 av_freep(&p_dirac_params->enc_buf); | |
398 | |
6734 | 399 /* free the input frame buffer */ |
400 av_freep(&p_dirac_params->p_in_frame_buf); | |
401 | |
402 return 0 ; | |
403 } | |
404 | |
405 | |
406 AVCodec libdirac_encoder = { | |
407 "libdirac", | |
408 CODEC_TYPE_VIDEO, | |
409 CODEC_ID_DIRAC, | |
410 sizeof(FfmpegDiracEncoderParams), | |
411 libdirac_encode_init, | |
412 libdirac_encode_frame, | |
413 libdirac_encode_close, | |
414 .capabilities= CODEC_CAP_DELAY, | |
8021
00fca5d6f0d9
Use PIX_FMT_NONE instead of -1 in AVCodec.pix_fmts.
cehoyos
parents:
7839
diff
changeset
|
415 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_NONE}, |
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
6819
diff
changeset
|
416 .long_name= NULL_IF_CONFIG_SMALL("libdirac Dirac 2.2"), |
6734 | 417 } ; |