00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00030 #include "libavutil/imgutils.h"
00031 #include "avcodec.h"
00032 #include "libdirac_libschro.h"
00033 #include "libschroedinger.h"
00034
00035 #undef NDEBUG
00036 #include <assert.h>
00037
00038
00039 #include <schroedinger/schro.h>
00040 #include <schroedinger/schrodebug.h>
00041 #include <schroedinger/schrovideoformat.h>
00042
00044 typedef struct SchroDecoderParams {
00046 SchroVideoFormat *format;
00047
00049 SchroFrameFormat frame_format;
00050
00052 SchroDecoder* decoder;
00053
00055 DiracSchroQueue dec_frame_queue;
00056
00058 int eos_signalled;
00059
00061 int eos_pulled;
00062
00064 AVPicture dec_pic;
00065 } SchroDecoderParams;
00066
00067 typedef struct SchroParseUnitContext {
00068 const uint8_t *buf;
00069 int buf_size;
00070 } SchroParseUnitContext;
00071
00072
00073 static void libschroedinger_decode_buffer_free(SchroBuffer *schro_buf,
00074 void *priv);
00075
00076 static void SchroParseContextInit(SchroParseUnitContext *parse_ctx,
00077 const uint8_t *buf, int buf_size)
00078 {
00079 parse_ctx->buf = buf;
00080 parse_ctx->buf_size = buf_size;
00081 }
00082
00083 static SchroBuffer *FindNextSchroParseUnit(SchroParseUnitContext *parse_ctx)
00084 {
00085 SchroBuffer *enc_buf = NULL;
00086 int next_pu_offset = 0;
00087 unsigned char *in_buf;
00088
00089 if (parse_ctx->buf_size < 13 ||
00090 parse_ctx->buf[0] != 'B' ||
00091 parse_ctx->buf[1] != 'B' ||
00092 parse_ctx->buf[2] != 'C' ||
00093 parse_ctx->buf[3] != 'D')
00094 return NULL;
00095
00096 next_pu_offset = (parse_ctx->buf[5] << 24) +
00097 (parse_ctx->buf[6] << 16) +
00098 (parse_ctx->buf[7] << 8) +
00099 parse_ctx->buf[8];
00100
00101 if (next_pu_offset == 0 &&
00102 SCHRO_PARSE_CODE_IS_END_OF_SEQUENCE(parse_ctx->buf[4]))
00103 next_pu_offset = 13;
00104
00105 if (next_pu_offset <= 0 || parse_ctx->buf_size < next_pu_offset)
00106 return NULL;
00107
00108 in_buf = av_malloc(next_pu_offset);
00109 memcpy(in_buf, parse_ctx->buf, next_pu_offset);
00110 enc_buf = schro_buffer_new_with_data(in_buf, next_pu_offset);
00111 enc_buf->free = libschroedinger_decode_buffer_free;
00112 enc_buf->priv = in_buf;
00113
00114 parse_ctx->buf += next_pu_offset;
00115 parse_ctx->buf_size -= next_pu_offset;
00116
00117 return enc_buf;
00118 }
00119
00123 static enum PixelFormat get_chroma_format(SchroChromaFormat schro_pix_fmt)
00124 {
00125 int num_formats = sizeof(schro_pixel_format_map) /
00126 sizeof(schro_pixel_format_map[0]);
00127 int idx;
00128
00129 for (idx = 0; idx < num_formats; ++idx)
00130 if (schro_pixel_format_map[idx].schro_pix_fmt == schro_pix_fmt)
00131 return schro_pixel_format_map[idx].ff_pix_fmt;
00132 return PIX_FMT_NONE;
00133 }
00134
00135 static av_cold int libschroedinger_decode_init(AVCodecContext *avccontext)
00136 {
00137
00138 SchroDecoderParams *p_schro_params = avccontext->priv_data;
00139
00140 schro_init();
00141
00142 schro_debug_set_level(avccontext->debug);
00143 p_schro_params->decoder = schro_decoder_new();
00144 schro_decoder_set_skip_ratio(p_schro_params->decoder, 1);
00145
00146 if (!p_schro_params->decoder)
00147 return -1;
00148
00149
00150 ff_dirac_schro_queue_init(&p_schro_params->dec_frame_queue);
00151 return 0;
00152 }
00153
00154 static void libschroedinger_decode_buffer_free(SchroBuffer *schro_buf,
00155 void *priv)
00156 {
00157 av_freep(&priv);
00158 }
00159
00160 static void libschroedinger_decode_frame_free(void *frame)
00161 {
00162 schro_frame_unref(frame);
00163 }
00164
00165 static void libschroedinger_handle_first_access_unit(AVCodecContext *avccontext)
00166 {
00167 SchroDecoderParams *p_schro_params = avccontext->priv_data;
00168 SchroDecoder *decoder = p_schro_params->decoder;
00169
00170 p_schro_params->format = schro_decoder_get_video_format(decoder);
00171
00172
00173 if (av_image_check_size(p_schro_params->format->width, p_schro_params->format->height,
00174 0, avccontext) < 0) {
00175 av_log(avccontext, AV_LOG_ERROR, "invalid dimensions (%dx%d)\n",
00176 p_schro_params->format->width, p_schro_params->format->height);
00177 avccontext->height = avccontext->width = 0;
00178 return;
00179 }
00180 avccontext->height = p_schro_params->format->height;
00181 avccontext->width = p_schro_params->format->width;
00182 avccontext->pix_fmt = get_chroma_format(p_schro_params->format->chroma_format);
00183
00184 if (ff_get_schro_frame_format(p_schro_params->format->chroma_format,
00185 &p_schro_params->frame_format) == -1) {
00186 av_log(avccontext, AV_LOG_ERROR,
00187 "This codec currently only supports planar YUV 4:2:0, 4:2:2 "
00188 "and 4:4:4 formats.\n");
00189 return;
00190 }
00191
00192 avccontext->time_base.den = p_schro_params->format->frame_rate_numerator;
00193 avccontext->time_base.num = p_schro_params->format->frame_rate_denominator;
00194
00195 if (!p_schro_params->dec_pic.data[0])
00196 avpicture_alloc(&p_schro_params->dec_pic,
00197 avccontext->pix_fmt,
00198 avccontext->width,
00199 avccontext->height);
00200 }
00201
00202 static int libschroedinger_decode_frame(AVCodecContext *avccontext,
00203 void *data, int *data_size,
00204 AVPacket *avpkt)
00205 {
00206 const uint8_t *buf = avpkt->data;
00207 int buf_size = avpkt->size;
00208
00209 SchroDecoderParams *p_schro_params = avccontext->priv_data;
00210 SchroDecoder *decoder = p_schro_params->decoder;
00211 AVPicture *picture = data;
00212 SchroBuffer *enc_buf;
00213 SchroFrame* frame;
00214 int state;
00215 int go = 1;
00216 int outer = 1;
00217 SchroParseUnitContext parse_ctx;
00218
00219 *data_size = 0;
00220
00221 SchroParseContextInit(&parse_ctx, buf, buf_size);
00222 if (!buf_size) {
00223 if (!p_schro_params->eos_signalled) {
00224 state = schro_decoder_push_end_of_stream(decoder);
00225 p_schro_params->eos_signalled = 1;
00226 }
00227 }
00228
00229
00230 do {
00231 if ((enc_buf = FindNextSchroParseUnit(&parse_ctx))) {
00232
00233 if (SCHRO_PARSE_CODE_IS_PICTURE(enc_buf->data[4]) &&
00234 SCHRO_PARSE_CODE_NUM_REFS(enc_buf->data[4]) > 0)
00235 avccontext->has_b_frames = 1;
00236 state = schro_decoder_push(decoder, enc_buf);
00237 if (state == SCHRO_DECODER_FIRST_ACCESS_UNIT)
00238 libschroedinger_handle_first_access_unit(avccontext);
00239 go = 1;
00240 } else
00241 outer = 0;
00242
00243 while (go) {
00244
00245 state = schro_decoder_wait(decoder);
00246 switch (state) {
00247 case SCHRO_DECODER_FIRST_ACCESS_UNIT:
00248 libschroedinger_handle_first_access_unit(avccontext);
00249 break;
00250
00251 case SCHRO_DECODER_NEED_BITS:
00252
00253 go = 0;
00254 break;
00255
00256 case SCHRO_DECODER_NEED_FRAME:
00257
00258 frame = ff_create_schro_frame(avccontext,
00259 p_schro_params->frame_format);
00260 schro_decoder_add_output_picture(decoder, frame);
00261 break;
00262
00263 case SCHRO_DECODER_OK:
00264
00265 frame = schro_decoder_pull(decoder);
00266
00267 if (frame)
00268 ff_dirac_schro_queue_push_back(&p_schro_params->dec_frame_queue,
00269 frame);
00270 break;
00271 case SCHRO_DECODER_EOS:
00272 go = 0;
00273 p_schro_params->eos_pulled = 1;
00274 schro_decoder_reset(decoder);
00275 outer = 0;
00276 break;
00277
00278 case SCHRO_DECODER_ERROR:
00279 return -1;
00280 break;
00281 }
00282 }
00283 } while (outer);
00284
00285
00286 frame = ff_dirac_schro_queue_pop(&p_schro_params->dec_frame_queue);
00287
00288 if (frame) {
00289 memcpy(p_schro_params->dec_pic.data[0],
00290 frame->components[0].data,
00291 frame->components[0].length);
00292
00293 memcpy(p_schro_params->dec_pic.data[1],
00294 frame->components[1].data,
00295 frame->components[1].length);
00296
00297 memcpy(p_schro_params->dec_pic.data[2],
00298 frame->components[2].data,
00299 frame->components[2].length);
00300
00301
00302 avpicture_fill(picture, p_schro_params->dec_pic.data[0],
00303 avccontext->pix_fmt,
00304 avccontext->width, avccontext->height);
00305
00306 *data_size = sizeof(AVPicture);
00307
00308
00309 libschroedinger_decode_frame_free(frame);
00310 }
00311 return buf_size;
00312 }
00313
00314
00315 static av_cold int libschroedinger_decode_close(AVCodecContext *avccontext)
00316 {
00317 SchroDecoderParams *p_schro_params = avccontext->priv_data;
00318
00319 schro_decoder_free(p_schro_params->decoder);
00320 av_freep(&p_schro_params->format);
00321
00322 avpicture_free(&p_schro_params->dec_pic);
00323
00324
00325 ff_dirac_schro_queue_free(&p_schro_params->dec_frame_queue,
00326 libschroedinger_decode_frame_free);
00327
00328 return 0;
00329 }
00330
00331 static void libschroedinger_flush(AVCodecContext *avccontext)
00332 {
00333
00334
00335 SchroDecoderParams *p_schro_params = avccontext->priv_data;
00336
00337
00338 ff_dirac_schro_queue_free(&p_schro_params->dec_frame_queue,
00339 libschroedinger_decode_frame_free);
00340
00341 ff_dirac_schro_queue_init(&p_schro_params->dec_frame_queue);
00342 schro_decoder_reset(p_schro_params->decoder);
00343 p_schro_params->eos_pulled = 0;
00344 p_schro_params->eos_signalled = 0;
00345 }
00346
00347 AVCodec ff_libschroedinger_decoder = {
00348 .name = "libschroedinger",
00349 .type = AVMEDIA_TYPE_VIDEO,
00350 .id = CODEC_ID_DIRAC,
00351 .priv_data_size = sizeof(SchroDecoderParams),
00352 .init = libschroedinger_decode_init,
00353 .close = libschroedinger_decode_close,
00354 .decode = libschroedinger_decode_frame,
00355 .capabilities = CODEC_CAP_DELAY,
00356 .flush = libschroedinger_flush,
00357 .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
00358 };