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