]> git.sesse.net Git - ffmpeg/blob - libavcodec/r210dec.c
Merge commit '9a07c1332cfe092b57b5758f22b686ca58806c60'
[ffmpeg] / libavcodec / r210dec.c
1 /*
2  * R210 decoder
3  *
4  * Copyright (c) 2009 Reimar Doeffinger <Reimar.Doeffinger@gmx.de>
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/bswap.h"
25 #include "libavutil/common.h"
26
27 static av_cold int decode_init(AVCodecContext *avctx)
28 {
29     avctx->pix_fmt             = AV_PIX_FMT_RGB48;
30     avctx->bits_per_raw_sample = 10;
31
32     avctx->coded_frame         = avcodec_alloc_frame();
33     if (!avctx->coded_frame)
34         return AVERROR(ENOMEM);
35
36     return 0;
37 }
38
39 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
40                         AVPacket *avpkt)
41 {
42     int h, w;
43     AVFrame *pic = avctx->coded_frame;
44     const uint32_t *src = (const uint32_t *)avpkt->data;
45     int aligned_width = FFALIGN(avctx->width,
46                                 avctx->codec_id == AV_CODEC_ID_R10K ? 1 : 64);
47     uint8_t *dst_line;
48
49     if (pic->data[0])
50         avctx->release_buffer(avctx, pic);
51
52     if (avpkt->size < 4 * aligned_width * avctx->height) {
53         av_log(avctx, AV_LOG_ERROR, "packet too small\n");
54         return -1;
55     }
56
57     pic->reference = 0;
58     if (avctx->get_buffer(avctx, pic) < 0)
59         return -1;
60
61     pic->pict_type = AV_PICTURE_TYPE_I;
62     pic->key_frame = 1;
63     dst_line = pic->data[0];
64
65     for (h = 0; h < avctx->height; h++) {
66         uint16_t *dst = (uint16_t *)dst_line;
67         for (w = 0; w < avctx->width; w++) {
68             uint32_t pixel;
69             uint16_t r, g, b;
70             if (avctx->codec_id==AV_CODEC_ID_AVRP) {
71                 pixel = av_le2ne32(*src++);
72             } else {
73                 pixel = av_be2ne32(*src++);
74             }
75             if (avctx->codec_id==AV_CODEC_ID_R210) {
76                 b =  pixel <<  6;
77                 g = (pixel >>  4) & 0xffc0;
78                 r = (pixel >> 14) & 0xffc0;
79             } else {
80                 b =  pixel <<  4;
81                 g = (pixel >>  6) & 0xffc0;
82                 r = (pixel >> 16) & 0xffc0;
83             }
84             *dst++ = r | (r >> 10);
85             *dst++ = g | (g >> 10);
86             *dst++ = b | (b >> 10);
87         }
88         src += aligned_width - avctx->width;
89         dst_line += pic->linesize[0];
90     }
91
92     *data_size = sizeof(AVFrame);
93     *(AVFrame*)data = *avctx->coded_frame;
94
95     return avpkt->size;
96 }
97
98 static av_cold int decode_close(AVCodecContext *avctx)
99 {
100     AVFrame *pic = avctx->coded_frame;
101     if (pic->data[0])
102         avctx->release_buffer(avctx, pic);
103     av_freep(&avctx->coded_frame);
104
105     return 0;
106 }
107
108 #if CONFIG_R210_DECODER
109 AVCodec ff_r210_decoder = {
110     .name           = "r210",
111     .type           = AVMEDIA_TYPE_VIDEO,
112     .id             = AV_CODEC_ID_R210,
113     .init           = decode_init,
114     .close          = decode_close,
115     .decode         = decode_frame,
116     .capabilities   = CODEC_CAP_DR1,
117     .long_name      = NULL_IF_CONFIG_SMALL("Uncompressed RGB 10-bit"),
118 };
119 #endif
120 #if CONFIG_R10K_DECODER
121 AVCodec ff_r10k_decoder = {
122     .name           = "r10k",
123     .type           = AVMEDIA_TYPE_VIDEO,
124     .id             = AV_CODEC_ID_R10K,
125     .init           = decode_init,
126     .close          = decode_close,
127     .decode         = decode_frame,
128     .capabilities   = CODEC_CAP_DR1,
129     .long_name      = NULL_IF_CONFIG_SMALL("AJA Kona 10-bit RGB Codec"),
130 };
131 #endif
132 #if CONFIG_AVRP_DECODER
133 AVCodec ff_avrp_decoder = {
134     .name           = "avrp",
135     .type           = AVMEDIA_TYPE_VIDEO,
136     .id             = AV_CODEC_ID_AVRP,
137     .init           = decode_init,
138     .close          = decode_close,
139     .decode         = decode_frame,
140     .capabilities   = CODEC_CAP_DR1,
141     .long_name = NULL_IF_CONFIG_SMALL("Avid 1:1 10-bit RGB Packer"),
142 };
143 #endif