]> git.sesse.net Git - ffmpeg/blob - libavcodec/avuidec.c
Merge remote-tracking branch 'qatar/master'
[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
25 static av_cold int avui_decode_init(AVCodecContext *avctx)
26 {
27     avctx->pix_fmt = PIX_FMT_YUVA422P;
28
29     avctx->coded_frame = avcodec_alloc_frame();
30
31     if (!avctx->coded_frame) {
32         av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
33         return AVERROR(ENOMEM);
34     }
35
36     return 0;
37 }
38
39 static int avui_decode_frame(AVCodecContext *avctx, void *data,
40                              int *data_size, AVPacket *avpkt)
41 {
42     AVFrame *pic = avctx->coded_frame;
43     const uint8_t *src = avpkt->data;
44     const uint8_t *srca;
45     uint8_t *y, *u, *v, *a;
46     int transparent, interlaced = 1, skip[2], i, j, k;
47
48     if (pic->data[0])
49         avctx->release_buffer(avctx, pic);
50
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) {
56         skip[0] =  8;
57         skip[1] = 12;
58     }
59     if (avpkt->size < avctx->width * (2 * avctx->height + skip[0] + skip[1])
60                       + 4 * interlaced) {
61         av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
62         return AVERROR(EINVAL);
63     }
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
68            + 5 + interlaced * 4;
69
70     pic->reference = 0;
71
72     if (avctx->get_buffer(avctx, pic) < 0) {
73         av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
74         return AVERROR(ENOMEM);
75     }
76
77     pic->key_frame = 1;
78     pic->pict_type = AV_PICTURE_TYPE_I;
79
80     if (!interlaced) {
81         src  += avctx->width * skip[1];
82         srca += avctx->width * skip[1];
83     }
84
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];
92
93         for (j = 0; j < avctx->height >> interlaced; j++) {
94             for (k = 0; k < avctx->width >> 1; k++) {
95                 u[    k    ] = *src++;
96                 y[2 * k    ] = *src++;
97                 a[2 * k    ] = 0xFF - (transparent ? *srca++ : 0);
98                 srca++;
99                 v[    k    ] = *src++;
100                 y[2 * k + 1] = *src++;
101                 a[2 * k + 1] = 0xFF - (transparent ? *srca++ : 0);
102                 srca++;
103             }
104
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];
109         }
110         src  += 4;
111         srca += 4;
112     }
113     *data_size = sizeof(AVFrame);
114     *(AVFrame *)data = *pic;
115
116     return avpkt->size;
117 }
118
119 static av_cold int avui_decode_close(AVCodecContext *avctx)
120 {
121     if (avctx->coded_frame->data[0])
122         avctx->release_buffer(avctx, avctx->coded_frame);
123
124     av_freep(&avctx->coded_frame);
125
126     return 0;
127 }
128
129 AVCodec ff_avui_decoder = {
130     .name         = "avui",
131     .type         = AVMEDIA_TYPE_VIDEO,
132     .id           = CODEC_ID_AVUI,
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"),
138 };