]> git.sesse.net Git - ffmpeg/blob - libavcodec/r210dec.c
lavc: move SANE_NB_CHANNELS to internal.h and use it in the PCM decoders
[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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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
34     return 0;
35 }
36
37 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
38                         AVPacket *avpkt)
39 {
40     int h, w;
41     AVFrame *pic = avctx->coded_frame;
42     const uint32_t *src = (const uint32_t *)avpkt->data;
43     int aligned_width = FFALIGN(avctx->width, 64);
44     uint8_t *dst_line;
45
46     if (pic->data[0])
47         avctx->release_buffer(avctx, pic);
48
49     if (avpkt->size < 4 * aligned_width * avctx->height) {
50         av_log(avctx, AV_LOG_ERROR, "packet too small\n");
51         return -1;
52     }
53
54     pic->reference = 0;
55     if (avctx->get_buffer(avctx, pic) < 0)
56         return -1;
57
58     pic->pict_type = AV_PICTURE_TYPE_I;
59     pic->key_frame = 1;
60     dst_line = pic->data[0];
61
62     for (h = 0; h < avctx->height; h++) {
63         uint16_t *dst = (uint16_t *)dst_line;
64         for (w = 0; w < avctx->width; w++) {
65             uint32_t pixel = av_be2ne32(*src++);
66             uint16_t r, g, b;
67             if (avctx->codec_id==AV_CODEC_ID_R210) {
68                 b =  pixel <<  6;
69                 g = (pixel >>  4) & 0xffc0;
70                 r = (pixel >> 14) & 0xffc0;
71             } else {
72                 b =  pixel <<  4;
73                 g = (pixel >>  6) & 0xffc0;
74                 r = (pixel >> 16) & 0xffc0;
75             }
76             *dst++ = r | (r >> 10);
77             *dst++ = g | (g >> 10);
78             *dst++ = b | (b >> 10);
79         }
80         src += aligned_width - avctx->width;
81         dst_line += pic->linesize[0];
82     }
83
84     *data_size = sizeof(AVFrame);
85     *(AVFrame*)data = *avctx->coded_frame;
86
87     return avpkt->size;
88 }
89
90 static av_cold int decode_close(AVCodecContext *avctx)
91 {
92     AVFrame *pic = avctx->coded_frame;
93     if (pic->data[0])
94         avctx->release_buffer(avctx, pic);
95     av_freep(&avctx->coded_frame);
96
97     return 0;
98 }
99
100 #if CONFIG_R210_DECODER
101 AVCodec ff_r210_decoder = {
102     .name           = "r210",
103     .type           = AVMEDIA_TYPE_VIDEO,
104     .id             = AV_CODEC_ID_R210,
105     .init           = decode_init,
106     .close          = decode_close,
107     .decode         = decode_frame,
108     .capabilities   = CODEC_CAP_DR1,
109     .long_name      = NULL_IF_CONFIG_SMALL("Uncompressed RGB 10-bit"),
110 };
111 #endif
112 #if CONFIG_R10K_DECODER
113 AVCodec ff_r10k_decoder = {
114     .name           = "r10k",
115     .type           = AVMEDIA_TYPE_VIDEO,
116     .id             = AV_CODEC_ID_R10K,
117     .init           = decode_init,
118     .close          = decode_close,
119     .decode         = decode_frame,
120     .capabilities   = CODEC_CAP_DR1,
121     .long_name      = NULL_IF_CONFIG_SMALL("AJA Kona 10-bit RGB Codec"),
122 };
123 #endif