]> 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, opaque_length, 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     if (avctx->height == 486) {
55         skip = 10;
56     } else {
57         skip = 16;
58     }
59     opaque_length = 2 * avctx->width * (avctx->height + skip) + 4 * interlaced;
60     if (avpkt->size < opaque_length) {
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 >= opaque_length * 2 + 4;
66     srca = src + opaque_length + 5;
67
68     pic->reference = 0;
69
70     if (avctx->get_buffer(avctx, pic) < 0) {
71         av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
72         return AVERROR(ENOMEM);
73     }
74
75     pic->key_frame = 1;
76     pic->pict_type = AV_PICTURE_TYPE_I;
77
78     if (!interlaced) {
79         src  += avctx->width * skip;
80         srca += avctx->width * skip;
81     }
82
83     for (i = 0; i < interlaced + 1; i++) {
84         src  += avctx->width * skip;
85         srca += avctx->width * skip;
86         if (interlaced && avctx->height == 486) {
87             y = pic->data[0] + (1 - i) * pic->linesize[0];
88             u = pic->data[1] + (1 - i) * pic->linesize[1];
89             v = pic->data[2] + (1 - i) * pic->linesize[2];
90             a = pic->data[3] + (1 - i) * pic->linesize[3];
91         } else {
92             y = pic->data[0] + i * pic->linesize[0];
93             u = pic->data[1] + i * pic->linesize[1];
94             v = pic->data[2] + i * pic->linesize[2];
95             a = pic->data[3] + i * pic->linesize[3];
96         }
97
98         for (j = 0; j < avctx->height >> interlaced; j++) {
99             for (k = 0; k < avctx->width >> 1; k++) {
100                 u[    k    ] = *src++;
101                 y[2 * k    ] = *src++;
102                 a[2 * k    ] = 0xFF - (transparent ? *srca++ : 0);
103                 srca++;
104                 v[    k    ] = *src++;
105                 y[2 * k + 1] = *src++;
106                 a[2 * k + 1] = 0xFF - (transparent ? *srca++ : 0);
107                 srca++;
108             }
109
110             y += (interlaced + 1) * pic->linesize[0];
111             u += (interlaced + 1) * pic->linesize[1];
112             v += (interlaced + 1) * pic->linesize[2];
113             a += (interlaced + 1) * pic->linesize[3];
114         }
115         src  += 4;
116         srca += 4;
117     }
118     *data_size = sizeof(AVFrame);
119     *(AVFrame *)data = *pic;
120
121     return avpkt->size;
122 }
123
124 static av_cold int avui_decode_close(AVCodecContext *avctx)
125 {
126     if (avctx->coded_frame->data[0])
127         avctx->release_buffer(avctx, avctx->coded_frame);
128
129     av_freep(&avctx->coded_frame);
130
131     return 0;
132 }
133
134 AVCodec ff_avui_decoder = {
135     .name         = "avui",
136     .type         = AVMEDIA_TYPE_VIDEO,
137     .id           = CODEC_ID_AVUI,
138     .init         = avui_decode_init,
139     .decode       = avui_decode_frame,
140     .close        = avui_decode_close,
141     .capabilities = CODEC_CAP_DR1,
142     .long_name    = NULL_IF_CONFIG_SMALL("AVID Meridien"),
143 };