]> git.sesse.net Git - ffmpeg/blob - libavcodec/avuidec.c
Add Avid Meridien (AVUI) decoder.
[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 = src + 2 * (avctx->height + 16) * avctx->width + 9;
45     uint8_t *y, *u, *v, *a;
46     int transparent = 0, i, j, k;
47
48     if (pic->data[0])
49         avctx->release_buffer(avctx, pic);
50
51     if (avpkt->size < 2 * avctx->width * (avctx->height + 16) + 4) {
52         av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
53         return AVERROR(EINVAL);
54     }
55     if (avpkt->size >= 4 * avctx->width * (avctx->height + 16) + 13)
56         transparent = 1;
57
58     pic->reference = 0;
59
60     if (avctx->get_buffer(avctx, pic) < 0) {
61         av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
62         return AVERROR(ENOMEM);
63     }
64
65     pic->key_frame = 1;
66     pic->pict_type = AV_PICTURE_TYPE_I;
67
68     for (i = 0; i < 2; i++) {
69         src  += avctx->width * 16;
70         srca += avctx->width * 16;
71         y = pic->data[0] + i * pic->linesize[0];
72         u = pic->data[1] + i * pic->linesize[1];
73         v = pic->data[2] + i * pic->linesize[2];
74         a = pic->data[3] + i * pic->linesize[3];
75
76         for (j = 0; j < (avctx->height + 1) >> 1; j++) {
77             for (k = 0; k < (avctx->width + 1) >> 1; k++) {
78                 u[    k    ] = *src++;
79                 y[2 * k    ] = *src++;
80                 a[2 * k    ] = 0xFF - (transparent ? *srca++ : 0);
81                 srca++;
82                 v[    k    ] = *src++;
83                 y[2 * k + 1] = *src++;
84                 a[2 * k + 1] = 0xFF - (transparent ? *srca++ : 0);
85                 srca++;
86             }
87
88             y += 2 * pic->linesize[0];
89             u += 2 * pic->linesize[1];
90             v += 2 * pic->linesize[2];
91             a += 2 * pic->linesize[3];
92         }
93         src  += 4;
94         srca += 4;
95     }
96     *data_size = sizeof(AVFrame);
97     *(AVFrame *)data = *pic;
98
99     return avpkt->size;
100 }
101
102 static av_cold int avui_decode_close(AVCodecContext *avctx)
103 {
104     if (avctx->coded_frame->data[0])
105         avctx->release_buffer(avctx, avctx->coded_frame);
106
107     av_freep(&avctx->coded_frame);
108
109     return 0;
110 }
111
112 AVCodec ff_avui_decoder = {
113     .name         = "avui",
114     .type         = AVMEDIA_TYPE_VIDEO,
115     .id           = CODEC_ID_AVUI,
116     .init         = avui_decode_init,
117     .decode       = avui_decode_frame,
118     .close        = avui_decode_close,
119     .capabilities = CODEC_CAP_DR1,
120     .long_name    = NULL_IF_CONFIG_SMALL("AVID Meridien"),
121 };