]> git.sesse.net Git - ffmpeg/blob - libavcodec/avuidec.c
Merge commit 'a65bdceb060628881578afb29df4eb222421381f'
[ffmpeg] / libavcodec / avuidec.c
1 /*
2  * AVID Meridien decoder
3  *
4  * Copyright (c) 2012 Carl Eugen Hoyos
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "avcodec.h"
24 #include "libavutil/intreadwrite.h"
25
26 static av_cold int avui_decode_init(AVCodecContext *avctx)
27 {
28     avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
29
30     avctx->coded_frame = avcodec_alloc_frame();
31
32     if (!avctx->coded_frame) {
33         av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
34         return AVERROR(ENOMEM);
35     }
36
37     return 0;
38 }
39
40 static int avui_decode_frame(AVCodecContext *avctx, void *data,
41                              int *data_size, AVPacket *avpkt)
42 {
43     AVFrame *pic = avctx->coded_frame;
44     const uint8_t *src = avpkt->data, *extradata = avctx->extradata;
45     const uint8_t *srca;
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;
49
50     if (pic->data[0])
51         avctx->release_buffer(avctx, pic);
52
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;
57             break;
58         }
59         if (atom_size && atom_size <= extradata_size) {
60             extradata      += atom_size;
61             extradata_size -= atom_size;
62         } else {
63             break;
64         }
65     }
66     if (avctx->height == 486) {
67         skip = 10;
68     } else {
69         skip = 16;
70     }
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);
75     }
76     transparent = avctx->bits_per_coded_sample == 32 &&
77                   avpkt->size >= opaque_length * 2 + 4;
78     srca = src + opaque_length + 5;
79
80     pic->reference = 0;
81
82     if (avctx->get_buffer(avctx, pic) < 0) {
83         av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
84         return AVERROR(ENOMEM);
85     }
86
87     pic->key_frame = 1;
88     pic->pict_type = AV_PICTURE_TYPE_I;
89
90     if (!interlaced) {
91         src  += avctx->width * skip;
92         srca += avctx->width * skip;
93     }
94
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];
103         } else {
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];
108         }
109
110         for (j = 0; j < avctx->height >> interlaced; j++) {
111             for (k = 0; k < avctx->width >> 1; k++) {
112                 u[    k    ] = *src++;
113                 y[2 * k    ] = *src++;
114                 a[2 * k    ] = 0xFF - (transparent ? *srca++ : 0);
115                 srca++;
116                 v[    k    ] = *src++;
117                 y[2 * k + 1] = *src++;
118                 a[2 * k + 1] = 0xFF - (transparent ? *srca++ : 0);
119                 srca++;
120             }
121
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];
126         }
127         src  += 4;
128         srca += 4;
129     }
130     *data_size = sizeof(AVFrame);
131     *(AVFrame *)data = *pic;
132
133     return avpkt->size;
134 }
135
136 static av_cold int avui_decode_close(AVCodecContext *avctx)
137 {
138     if (avctx->coded_frame->data[0])
139         avctx->release_buffer(avctx, avctx->coded_frame);
140
141     av_freep(&avctx->coded_frame);
142
143     return 0;
144 }
145
146 AVCodec ff_avui_decoder = {
147     .name         = "avui",
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"),
155 };