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