2 * AVID Meridien decoder
4 * Copyright (c) 2012 Carl Eugen Hoyos
6 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/intreadwrite.h"
26 static av_cold int avui_decode_init(AVCodecContext *avctx)
28 avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
30 avctx->coded_frame = avcodec_alloc_frame();
32 if (!avctx->coded_frame) {
33 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
34 return AVERROR(ENOMEM);
40 static int avui_decode_frame(AVCodecContext *avctx, void *data,
41 int *data_size, AVPacket *avpkt)
43 AVFrame *pic = avctx->coded_frame;
44 const uint8_t *src = avpkt->data, *extradata = avctx->extradata;
46 uint8_t *y, *u, *v, *a;
47 int transparent, interlaced = 1, skip, opaque_length, i, j, k;
48 uint32_t extradata_size = avctx->extradata_size;
51 avctx->release_buffer(avctx, pic);
53 while (extradata_size >= 24) {
54 uint32_t atom_size = AV_RB32(extradata);
55 if (!memcmp(&extradata[4], "APRGAPRG0001", 12)) {
56 interlaced = extradata[19] != 1;
59 if (atom_size && atom_size <= extradata_size) {
60 extradata += atom_size;
61 extradata_size -= atom_size;
66 if (avctx->height == 486) {
71 opaque_length = 2 * avctx->width * (avctx->height + skip) + 4 * interlaced;
72 if (avpkt->size < opaque_length) {
73 av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
74 return AVERROR(EINVAL);
76 transparent = avctx->bits_per_coded_sample == 32 &&
77 avpkt->size >= opaque_length * 2 + 4;
78 srca = src + opaque_length + 5;
82 if (avctx->get_buffer(avctx, pic) < 0) {
83 av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
84 return AVERROR(ENOMEM);
88 pic->pict_type = AV_PICTURE_TYPE_I;
91 src += avctx->width * skip;
92 srca += avctx->width * skip;
95 for (i = 0; i < interlaced + 1; i++) {
96 src += avctx->width * skip;
97 srca += avctx->width * skip;
98 if (interlaced && avctx->height == 486) {
99 y = pic->data[0] + (1 - i) * pic->linesize[0];
100 u = pic->data[1] + (1 - i) * pic->linesize[1];
101 v = pic->data[2] + (1 - i) * pic->linesize[2];
102 a = pic->data[3] + (1 - i) * pic->linesize[3];
104 y = pic->data[0] + i * pic->linesize[0];
105 u = pic->data[1] + i * pic->linesize[1];
106 v = pic->data[2] + i * pic->linesize[2];
107 a = pic->data[3] + i * pic->linesize[3];
110 for (j = 0; j < avctx->height >> interlaced; j++) {
111 for (k = 0; k < avctx->width >> 1; k++) {
114 a[2 * k ] = 0xFF - (transparent ? *srca++ : 0);
117 y[2 * k + 1] = *src++;
118 a[2 * k + 1] = 0xFF - (transparent ? *srca++ : 0);
122 y += (interlaced + 1) * pic->linesize[0];
123 u += (interlaced + 1) * pic->linesize[1];
124 v += (interlaced + 1) * pic->linesize[2];
125 a += (interlaced + 1) * pic->linesize[3];
130 *data_size = sizeof(AVFrame);
131 *(AVFrame *)data = *pic;
136 static av_cold int avui_decode_close(AVCodecContext *avctx)
138 if (avctx->coded_frame->data[0])
139 avctx->release_buffer(avctx, avctx->coded_frame);
141 av_freep(&avctx->coded_frame);
146 AVCodec ff_avui_decoder = {
148 .type = AVMEDIA_TYPE_VIDEO,
149 .id = AV_CODEC_ID_AVUI,
150 .init = avui_decode_init,
151 .decode = avui_decode_frame,
152 .close = avui_decode_close,
153 .capabilities = CODEC_CAP_DR1,
154 .long_name = NULL_IF_CONFIG_SMALL("Avid Meridien Uncompressed"),