]> git.sesse.net Git - ffmpeg/blob - libavcodec/dpxenc.c
proresdec: Fix read via negative index in a global array.
[ffmpeg] / libavcodec / dpxenc.c
1 /*
2  * DPX (.dpx) image encoder
3  * Copyright (c) 2011 Peter Ross <pross@xvid.org>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/imgutils.h"
24 #include "avcodec.h"
25
26 typedef struct DPXContext {
27     AVFrame picture;
28     int big_endian;
29     int bits_per_component;
30     int descriptor;
31 } DPXContext;
32
33 static av_cold int encode_init(AVCodecContext *avctx)
34 {
35     DPXContext *s = avctx->priv_data;
36
37     avctx->coded_frame = &s->picture;
38     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
39     avctx->coded_frame->key_frame = 1;
40
41     s->big_endian         = 1;
42     s->bits_per_component = 8;
43     s->descriptor         = 50; /* RGB */
44
45     switch (avctx->pix_fmt) {
46     case PIX_FMT_RGB24:
47         break;
48     case PIX_FMT_RGBA:
49         s->descriptor = 51; /* RGBA */
50         break;
51     case PIX_FMT_RGB48LE:
52         s->big_endian = 0;
53     case PIX_FMT_RGB48BE:
54         s->bits_per_component = avctx->bits_per_raw_sample ? avctx->bits_per_raw_sample : 16;
55         break;
56     case PIX_FMT_RGBA64LE:
57         s->big_endian = 0;
58     case PIX_FMT_RGBA64BE:
59         s->descriptor = 51;
60         s->bits_per_component = 16;
61         break;
62     default:
63         av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
64         return -1;
65     }
66
67     return 0;
68 }
69
70 #define write16(p, value) \
71 do { \
72     if (s->big_endian) AV_WB16(p, value); \
73     else               AV_WL16(p, value); \
74 } while(0)
75
76 #define write32(p, value) \
77 do { \
78     if (s->big_endian) AV_WB32(p, value); \
79     else               AV_WL32(p, value); \
80 } while(0)
81
82 static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic, uint8_t *dst)
83 {
84     DPXContext *s = avctx->priv_data;
85     const uint8_t *src = pic->data[0];
86     int x, y;
87
88     for (y = 0; y < avctx->height; y++) {
89         for (x = 0; x < avctx->width; x++) {
90             int value;
91             if ((avctx->pix_fmt & 1)) {
92                 value = ((AV_RB16(src + 6*x + 4) & 0xFFC0) >> 4)
93                       | ((AV_RB16(src + 6*x + 2) & 0xFFC0) << 6)
94                       | ((AV_RB16(src + 6*x + 0) & 0xFFC0) << 16);
95             } else {
96                 value = ((AV_RL16(src + 6*x + 4) & 0xFFC0) >> 4)
97                       | ((AV_RL16(src + 6*x + 2) & 0xFFC0) << 6)
98                       | ((AV_RL16(src + 6*x + 0) & 0xFFC0) << 16);
99             }
100             write32(dst, value);
101             dst += 4;
102         }
103         src += pic->linesize[0];
104     }
105 }
106
107 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data)
108 {
109     DPXContext *s = avctx->priv_data;
110     int size;
111
112 #define HEADER_SIZE 1664  /* DPX Generic header */
113     if (buf_size < HEADER_SIZE)
114         return -1;
115
116     memset(buf, 0, HEADER_SIZE);
117
118     /* File information header */
119     write32(buf,       MKBETAG('S','D','P','X'));
120     write32(buf +   4, HEADER_SIZE);
121     memcpy (buf +   8, "V1.0", 4);
122     write32(buf +  20, 1); /* new image */
123     write32(buf +  24, HEADER_SIZE);
124     if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
125         memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
126     }
127     write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
128
129     /* Image information header */
130     write16(buf + 768, 0); /* orientation; left to right, top to bottom */
131     write16(buf + 770, 1); /* number of elements */
132     write32(buf + 772, avctx->width);
133     write32(buf + 776, avctx->height);
134     buf[800] = s->descriptor;
135     buf[801] = 2; /* linear transfer */
136     buf[802] = 2; /* linear colorimetric */
137     buf[803] = s->bits_per_component;
138     write16(buf + 804, s->bits_per_component == 10 ? 1 : 0); /* packing method */
139
140     /* Image source information header */
141     write32(buf + 1628, avctx->sample_aspect_ratio.num);
142     write32(buf + 1632, avctx->sample_aspect_ratio.den);
143
144     switch(s->bits_per_component) {
145     case 8:
146     case 16:
147         size = avpicture_layout(data, avctx->pix_fmt,
148                                 avctx->width, avctx->height,
149                                 buf + HEADER_SIZE, buf_size - HEADER_SIZE);
150         if (size < 0)
151             return size;
152         break;
153     case 10:
154         size = avctx->height * avctx->width * 4;
155         if (buf_size < HEADER_SIZE + size)
156             return -1;
157         encode_rgb48_10bit(avctx, data, buf + HEADER_SIZE);
158         break;
159     default:
160         av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
161         return -1;
162     }
163
164     size += HEADER_SIZE;
165
166     write32(buf + 16, size); /* file size */
167     return size;
168 }
169
170 AVCodec ff_dpx_encoder = {
171     .name = "dpx",
172     .type = AVMEDIA_TYPE_VIDEO,
173     .id   = CODEC_ID_DPX,
174     .priv_data_size = sizeof(DPXContext),
175     .init   = encode_init,
176     .encode = encode_frame,
177     .pix_fmts = (const enum PixelFormat[]){
178         PIX_FMT_RGB24,
179         PIX_FMT_RGBA,
180         PIX_FMT_RGB48LE,
181         PIX_FMT_RGB48BE,
182         PIX_FMT_RGBA64LE,
183         PIX_FMT_RGBA64BE,
184         PIX_FMT_NONE},
185     .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
186 };