comparison libschroedingerdec.c @ 10060:965220ebc611 libavcodec

cosmetics: indentation, prettyprinting, K&R coding style
author diego
date Sat, 15 Aug 2009 11:42:15 +0000
parents 646065f63290
children 09f2db2d7c90
comparison
equal deleted inserted replaced
10059:c6ace59701ce 10060:965220ebc611
38 #include <schroedinger/schro.h> 38 #include <schroedinger/schro.h>
39 #include <schroedinger/schrodebug.h> 39 #include <schroedinger/schrodebug.h>
40 #include <schroedinger/schrovideoformat.h> 40 #include <schroedinger/schrovideoformat.h>
41 41
42 /** libschroedinger decoder private data */ 42 /** libschroedinger decoder private data */
43 typedef struct FfmpegSchroDecoderParams 43 typedef struct FfmpegSchroDecoderParams {
44 {
45 /** Schroedinger video format */ 44 /** Schroedinger video format */
46 SchroVideoFormat *format; 45 SchroVideoFormat *format;
47 46
48 /** Schroedinger frame format */ 47 /** Schroedinger frame format */
49 SchroFrameFormat frame_format; 48 SchroFrameFormat frame_format;
62 61
63 /** decoded picture */ 62 /** decoded picture */
64 AVPicture dec_pic; 63 AVPicture dec_pic;
65 } FfmpegSchroDecoderParams; 64 } FfmpegSchroDecoderParams;
66 65
67 typedef struct FfmpegSchroParseUnitContext 66 typedef struct FfmpegSchroParseUnitContext {
68 {
69 const uint8_t *buf; 67 const uint8_t *buf;
70 int buf_size; 68 int buf_size;
71 } FfmpegSchroParseUnitContext; 69 } FfmpegSchroParseUnitContext;
72 70
73 71
74 static void libschroedinger_decode_buffer_free (SchroBuffer *schro_buf, 72 static void libschroedinger_decode_buffer_free(SchroBuffer *schro_buf,
75 void *priv); 73 void *priv);
76 74
77 static void FfmpegSchroParseContextInit (FfmpegSchroParseUnitContext *parse_ctx, 75 static void FfmpegSchroParseContextInit(FfmpegSchroParseUnitContext *parse_ctx,
78 const uint8_t *buf, int buf_size) 76 const uint8_t *buf, int buf_size)
79 { 77 {
80 parse_ctx->buf = buf; 78 parse_ctx->buf = buf;
81 parse_ctx->buf_size = buf_size; 79 parse_ctx->buf_size = buf_size;
82 } 80 }
83 81
84 static SchroBuffer* FfmpegFindNextSchroParseUnit (FfmpegSchroParseUnitContext *parse_ctx) 82 static SchroBuffer* FfmpegFindNextSchroParseUnit(FfmpegSchroParseUnitContext *parse_ctx)
85 { 83 {
86 SchroBuffer *enc_buf = NULL; 84 SchroBuffer *enc_buf = NULL;
87 int next_pu_offset = 0; 85 int next_pu_offset = 0;
88 unsigned char *in_buf; 86 unsigned char *in_buf;
89 87
105 103
106 if (next_pu_offset <= 0 || parse_ctx->buf_size < next_pu_offset) 104 if (next_pu_offset <= 0 || parse_ctx->buf_size < next_pu_offset)
107 return NULL; 105 return NULL;
108 106
109 in_buf = av_malloc(next_pu_offset); 107 in_buf = av_malloc(next_pu_offset);
110 memcpy (in_buf, parse_ctx->buf, next_pu_offset); 108 memcpy(in_buf, parse_ctx->buf, next_pu_offset);
111 enc_buf = schro_buffer_new_with_data (in_buf, next_pu_offset); 109 enc_buf = schro_buffer_new_with_data(in_buf, next_pu_offset);
112 enc_buf->free = libschroedinger_decode_buffer_free; 110 enc_buf->free = libschroedinger_decode_buffer_free;
113 enc_buf->priv = in_buf; 111 enc_buf->priv = in_buf;
114 112
115 parse_ctx->buf += next_pu_offset; 113 parse_ctx->buf += next_pu_offset;
116 parse_ctx->buf_size -= next_pu_offset; 114 parse_ctx->buf_size -= next_pu_offset;
117 115
118 return enc_buf; 116 return enc_buf;
119 } 117 }
120 118
134 } 132 }
135 133
136 static av_cold int libschroedinger_decode_init(AVCodecContext *avccontext) 134 static av_cold int libschroedinger_decode_init(AVCodecContext *avccontext)
137 { 135 {
138 136
139 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data ; 137 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
140 /* First of all, initialize our supporting libraries. */ 138 /* First of all, initialize our supporting libraries. */
141 schro_init(); 139 schro_init();
142 140
143 schro_debug_set_level(avccontext->debug); 141 schro_debug_set_level(avccontext->debug);
144 p_schro_params->decoder = schro_decoder_new(); 142 p_schro_params->decoder = schro_decoder_new();
145 schro_decoder_set_skip_ratio(p_schro_params->decoder, 1); 143 schro_decoder_set_skip_ratio(p_schro_params->decoder, 1);
146 144
147 if (!p_schro_params->decoder) 145 if (!p_schro_params->decoder)
148 return -1; 146 return -1;
149 147
150 /* Initialize the decoded frame queue. */ 148 /* Initialize the decoded frame queue. */
151 ff_dirac_schro_queue_init (&p_schro_params->dec_frame_queue); 149 ff_dirac_schro_queue_init(&p_schro_params->dec_frame_queue);
152 return 0 ; 150 return 0;
153 } 151 }
154 152
155 static void libschroedinger_decode_buffer_free (SchroBuffer *schro_buf, 153 static void libschroedinger_decode_buffer_free(SchroBuffer *schro_buf,
156 void *priv) 154 void *priv)
157 { 155 {
158 av_freep(&priv); 156 av_freep(&priv);
159 } 157 }
160 158
161 static void libschroedinger_decode_frame_free (void *frame) 159 static void libschroedinger_decode_frame_free(void *frame)
162 { 160 {
163 schro_frame_unref(frame); 161 schro_frame_unref(frame);
164 } 162 }
165 163
166 static void libschroedinger_handle_first_access_unit(AVCodecContext *avccontext) 164 static void libschroedinger_handle_first_access_unit(AVCodecContext *avccontext)
167 { 165 {
168 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data; 166 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
169 SchroDecoder *decoder = p_schro_params->decoder; 167 SchroDecoder *decoder = p_schro_params->decoder;
170 168
171 p_schro_params->format = schro_decoder_get_video_format (decoder); 169 p_schro_params->format = schro_decoder_get_video_format(decoder);
172 170
173 /* Tell FFmpeg about sequence details. */ 171 /* Tell FFmpeg about sequence details. */
174 if(avcodec_check_dimensions(avccontext, p_schro_params->format->width, 172 if (avcodec_check_dimensions(avccontext, p_schro_params->format->width,
175 p_schro_params->format->height) < 0) { 173 p_schro_params->format->height) < 0) {
176 av_log(avccontext, AV_LOG_ERROR, "invalid dimensions (%dx%d)\n", 174 av_log(avccontext, AV_LOG_ERROR, "invalid dimensions (%dx%d)\n",
177 p_schro_params->format->width, p_schro_params->format->height); 175 p_schro_params->format->width, p_schro_params->format->height);
178 avccontext->height = avccontext->width = 0; 176 avccontext->height = avccontext->width = 0;
179 return; 177 return;
180 } 178 }
181 avccontext->height = p_schro_params->format->height; 179 avccontext->height = p_schro_params->format->height;
182 avccontext->width = p_schro_params->format->width; 180 avccontext->width = p_schro_params->format->width;
183 avccontext->pix_fmt = 181 avccontext->pix_fmt = GetFfmpegChromaFormat(p_schro_params->format->chroma_format);
184 GetFfmpegChromaFormat(p_schro_params->format->chroma_format); 182
185 183 if (ff_get_schro_frame_format(p_schro_params->format->chroma_format,
186 if (ff_get_schro_frame_format( p_schro_params->format->chroma_format, 184 &p_schro_params->frame_format) == -1) {
187 &p_schro_params->frame_format) == -1) { 185 av_log(avccontext, AV_LOG_ERROR,
188 av_log (avccontext, AV_LOG_ERROR, 186 "This codec currently only supports planar YUV 4:2:0, 4:2:2 "
189 "This codec currently only supports planar YUV 4:2:0, 4:2:2 " 187 "and 4:4:4 formats.\n");
190 "and 4:4:4 formats.\n");
191 return; 188 return;
192 } 189 }
193 190
194 avccontext->time_base.den = p_schro_params->format->frame_rate_numerator; 191 avccontext->time_base.den = p_schro_params->format->frame_rate_numerator;
195 avccontext->time_base.num = p_schro_params->format->frame_rate_denominator; 192 avccontext->time_base.num = p_schro_params->format->frame_rate_denominator;
219 int outer = 1; 216 int outer = 1;
220 FfmpegSchroParseUnitContext parse_ctx; 217 FfmpegSchroParseUnitContext parse_ctx;
221 218
222 *data_size = 0; 219 *data_size = 0;
223 220
224 FfmpegSchroParseContextInit (&parse_ctx, buf, buf_size); 221 FfmpegSchroParseContextInit(&parse_ctx, buf, buf_size);
225 if (!buf_size) { 222 if (!buf_size) {
226 if (!p_schro_params->eos_signalled) { 223 if (!p_schro_params->eos_signalled) {
227 state = schro_decoder_push_end_of_stream(decoder); 224 state = schro_decoder_push_end_of_stream(decoder);
228 p_schro_params->eos_signalled = 1; 225 p_schro_params->eos_signalled = 1;
229 } 226 }
234 if ((enc_buf = FfmpegFindNextSchroParseUnit(&parse_ctx))) { 231 if ((enc_buf = FfmpegFindNextSchroParseUnit(&parse_ctx))) {
235 /* Push buffer into decoder. */ 232 /* Push buffer into decoder. */
236 if (SCHRO_PARSE_CODE_IS_PICTURE(enc_buf->data[4]) && 233 if (SCHRO_PARSE_CODE_IS_PICTURE(enc_buf->data[4]) &&
237 SCHRO_PARSE_CODE_NUM_REFS(enc_buf->data[4]) > 0) 234 SCHRO_PARSE_CODE_NUM_REFS(enc_buf->data[4]) > 0)
238 avccontext->has_b_frames = 1; 235 avccontext->has_b_frames = 1;
239 state = schro_decoder_push (decoder, enc_buf); 236 state = schro_decoder_push(decoder, enc_buf);
240 if (state == SCHRO_DECODER_FIRST_ACCESS_UNIT) 237 if (state == SCHRO_DECODER_FIRST_ACCESS_UNIT)
241 libschroedinger_handle_first_access_unit(avccontext); 238 libschroedinger_handle_first_access_unit(avccontext);
242 go = 1; 239 go = 1;
240 } else
241 outer = 0;
242 format = p_schro_params->format;
243
244 while (go) {
245 /* Parse data and process result. */
246 state = schro_decoder_wait(decoder);
247 switch (state) {
248 case SCHRO_DECODER_FIRST_ACCESS_UNIT:
249 libschroedinger_handle_first_access_unit(avccontext);
250 break;
251
252 case SCHRO_DECODER_NEED_BITS:
253 /* Need more input data - stop iterating over what we have. */
254 go = 0;
255 break;
256
257 case SCHRO_DECODER_NEED_FRAME:
258 /* Decoder needs a frame - create one and push it in. */
259
260 frame = schro_frame_new_and_alloc(NULL,
261 p_schro_params->frame_format,
262 format->width,
263 format->height);
264 schro_decoder_add_output_picture(decoder, frame);
265 break;
266
267 case SCHRO_DECODER_OK:
268 /* Pull a frame out of the decoder. */
269 frame = schro_decoder_pull(decoder);
270
271 if (frame)
272 ff_dirac_schro_queue_push_back(&p_schro_params->dec_frame_queue,
273 frame);
274 break;
275 case SCHRO_DECODER_EOS:
276 go = 0;
277 p_schro_params->eos_pulled = 1;
278 schro_decoder_reset(decoder);
279 outer = 0;
280 break;
281
282 case SCHRO_DECODER_ERROR:
283 return -1;
284 break;
285 }
243 } 286 }
244 else 287 } while (outer);
245 outer = 0;
246 format = p_schro_params->format;
247
248 while (go) {
249 /* Parse data and process result. */
250 state = schro_decoder_wait (decoder);
251 switch (state)
252 {
253 case SCHRO_DECODER_FIRST_ACCESS_UNIT:
254 libschroedinger_handle_first_access_unit (avccontext);
255 break;
256
257 case SCHRO_DECODER_NEED_BITS:
258 /* Need more input data - stop iterating over what we have. */
259 go = 0;
260 break;
261
262 case SCHRO_DECODER_NEED_FRAME:
263 /* Decoder needs a frame - create one and push it in. */
264
265 frame = schro_frame_new_and_alloc(NULL,
266 p_schro_params->frame_format,
267 format->width,
268 format->height);
269 schro_decoder_add_output_picture (decoder, frame);
270 break;
271
272 case SCHRO_DECODER_OK:
273 /* Pull a frame out of the decoder. */
274 frame = schro_decoder_pull (decoder);
275
276 if (frame)
277 ff_dirac_schro_queue_push_back(
278 &p_schro_params->dec_frame_queue,
279 frame);
280 break;
281 case SCHRO_DECODER_EOS:
282 go = 0;
283 p_schro_params->eos_pulled = 1;
284 schro_decoder_reset (decoder);
285 outer = 0;
286 break;
287
288 case SCHRO_DECODER_ERROR:
289 return -1;
290 break;
291 }
292 }
293 } while(outer);
294 288
295 /* Grab next frame to be returned from the top of the queue. */ 289 /* Grab next frame to be returned from the top of the queue. */
296 frame = ff_dirac_schro_queue_pop(&p_schro_params->dec_frame_queue); 290 frame = ff_dirac_schro_queue_pop(&p_schro_params->dec_frame_queue);
297 291
298 if (frame) { 292 if (frame) {
299 memcpy (p_schro_params->dec_pic.data[0], 293 memcpy(p_schro_params->dec_pic.data[0],
300 frame->components[0].data, 294 frame->components[0].data,
301 frame->components[0].length); 295 frame->components[0].length);
302 296
303 memcpy (p_schro_params->dec_pic.data[1], 297 memcpy(p_schro_params->dec_pic.data[1],
304 frame->components[1].data, 298 frame->components[1].data,
305 frame->components[1].length); 299 frame->components[1].length);
306 300
307 memcpy (p_schro_params->dec_pic.data[2], 301 memcpy(p_schro_params->dec_pic.data[2],
308 frame->components[2].data, 302 frame->components[2].data,
309 frame->components[2].length); 303 frame->components[2].length);
310 304
311 /* Fill picture with current buffer data from Schroedinger. */ 305 /* Fill picture with current buffer data from Schroedinger. */
312 avpicture_fill(picture, p_schro_params->dec_pic.data[0], 306 avpicture_fill(picture, p_schro_params->dec_pic.data[0],
313 avccontext->pix_fmt, 307 avccontext->pix_fmt,
314 avccontext->width, avccontext->height); 308 avccontext->width, avccontext->height);
315 309
316 *data_size = sizeof(AVPicture); 310 *data_size = sizeof(AVPicture);
317 311
318 /* Now free the frame resources. */ 312 /* Now free the frame resources. */
319 libschroedinger_decode_frame_free (frame); 313 libschroedinger_decode_frame_free(frame);
320 } 314 }
321 return buf_size; 315 return buf_size;
322 } 316 }
323 317
324 318
325 static av_cold int libschroedinger_decode_close(AVCodecContext *avccontext) 319 static av_cold int libschroedinger_decode_close(AVCodecContext *avccontext)
326 { 320 {
327 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data; 321 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
328 /* Free the decoder. */ 322 /* Free the decoder. */
329 schro_decoder_free (p_schro_params->decoder); 323 schro_decoder_free(p_schro_params->decoder);
330 av_freep(&p_schro_params->format); 324 av_freep(&p_schro_params->format);
331 325
332 avpicture_free (&p_schro_params->dec_pic); 326 avpicture_free(&p_schro_params->dec_pic);
333 327
334 /* Free data in the output frame queue. */ 328 /* Free data in the output frame queue. */
335 ff_dirac_schro_queue_free (&p_schro_params->dec_frame_queue, 329 ff_dirac_schro_queue_free(&p_schro_params->dec_frame_queue,
336 libschroedinger_decode_frame_free); 330 libschroedinger_decode_frame_free);
337 331
338 return 0 ; 332 return 0;
339 } 333 }
340 334
341 static void libschroedinger_flush (AVCodecContext *avccontext) 335 static void libschroedinger_flush(AVCodecContext *avccontext)
342 { 336 {
343 /* Got a seek request. Free the decoded frames queue and then reset 337 /* Got a seek request. Free the decoded frames queue and then reset
344 * the decoder */ 338 * the decoder */
345 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data; 339 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
346 340
347 /* Free data in the output frame queue. */ 341 /* Free data in the output frame queue. */
348 ff_dirac_schro_queue_free (&p_schro_params->dec_frame_queue, 342 ff_dirac_schro_queue_free(&p_schro_params->dec_frame_queue,
349 libschroedinger_decode_frame_free); 343 libschroedinger_decode_frame_free);
350 344
351 ff_dirac_schro_queue_init (&p_schro_params->dec_frame_queue); 345 ff_dirac_schro_queue_init(&p_schro_params->dec_frame_queue);
352 schro_decoder_reset(p_schro_params->decoder); 346 schro_decoder_reset(p_schro_params->decoder);
353 p_schro_params->eos_pulled = 0; 347 p_schro_params->eos_pulled = 0;
354 p_schro_params->eos_signalled = 0; 348 p_schro_params->eos_signalled = 0;
355 } 349 }
356 350
357 AVCodec libschroedinger_decoder = { 351 AVCodec libschroedinger_decoder = {
358 "libschroedinger", 352 "libschroedinger",
359 CODEC_TYPE_VIDEO, 353 CODEC_TYPE_VIDEO,
360 CODEC_ID_DIRAC, 354 CODEC_ID_DIRAC,
361 sizeof(FfmpegSchroDecoderParams), 355 sizeof(FfmpegSchroDecoderParams),
362 libschroedinger_decode_init, 356 libschroedinger_decode_init,
363 NULL, 357 NULL,