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
25 static av_cold int avui_decode_init(AVCodecContext *avctx)
27 avctx->pix_fmt = PIX_FMT_YUVA422P;
29 avctx->coded_frame = avcodec_alloc_frame();
31 if (!avctx->coded_frame) {
32 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
33 return AVERROR(ENOMEM);
39 static int avui_decode_frame(AVCodecContext *avctx, void *data,
40 int *data_size, AVPacket *avpkt)
42 AVFrame *pic = avctx->coded_frame;
43 const uint8_t *src = avpkt->data;
45 uint8_t *y, *u, *v, *a;
46 int transparent, interlaced = 1, skip[2], i, j, k;
49 avctx->release_buffer(avctx, pic);
51 if (!memcmp(&avctx->extradata[4], "APRGAPRG0001", 12) &&
52 avctx->extradata_size >= 24)
53 interlaced = avctx->extradata[19] != 1;
54 skip[0] = skip[1] = 16;
55 if (avctx->height == 486) {
59 if (avpkt->size < avctx->width * (2 * avctx->height + skip[0] + skip[1])
61 av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
62 return AVERROR(EINVAL);
64 transparent = avctx->bits_per_coded_sample == 32 &&
65 avpkt->size >= (2 * avctx->height + skip[0] + skip[1]) *
66 2 * avctx->width + 4 + 8 * interlaced;
67 srca = src + (2 * avctx->height + skip[0] + skip[1]) * avctx->width
72 if (avctx->get_buffer(avctx, pic) < 0) {
73 av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
74 return AVERROR(ENOMEM);
78 pic->pict_type = AV_PICTURE_TYPE_I;
81 src += avctx->width * skip[1];
82 srca += avctx->width * skip[1];
85 for (i = 0; i < interlaced + 1; i++) {
86 src += avctx->width * skip[i];
87 srca += avctx->width * skip[i];
88 y = pic->data[0] + i * pic->linesize[0];
89 u = pic->data[1] + i * pic->linesize[1];
90 v = pic->data[2] + i * pic->linesize[2];
91 a = pic->data[3] + i * pic->linesize[3];
93 for (j = 0; j < avctx->height >> interlaced; j++) {
94 for (k = 0; k < avctx->width >> 1; k++) {
97 a[2 * k ] = 0xFF - (transparent ? *srca++ : 0);
100 y[2 * k + 1] = *src++;
101 a[2 * k + 1] = 0xFF - (transparent ? *srca++ : 0);
105 y += (interlaced + 1) * pic->linesize[0];
106 u += (interlaced + 1) * pic->linesize[1];
107 v += (interlaced + 1) * pic->linesize[2];
108 a += (interlaced + 1) * pic->linesize[3];
113 *data_size = sizeof(AVFrame);
114 *(AVFrame *)data = *pic;
119 static av_cold int avui_decode_close(AVCodecContext *avctx)
121 if (avctx->coded_frame->data[0])
122 avctx->release_buffer(avctx, avctx->coded_frame);
124 av_freep(&avctx->coded_frame);
129 AVCodec ff_avui_decoder = {
131 .type = AVMEDIA_TYPE_VIDEO,
133 .init = avui_decode_init,
134 .decode = avui_decode_frame,
135 .close = avui_decode_close,
136 .capabilities = CODEC_CAP_DR1,
137 .long_name = NULL_IF_CONFIG_SMALL("AVID Meridien"),