]> git.sesse.net Git - ffmpeg/blob - libavcodec/dpxenc.c
decode: be more explicit about storing the last packet properties
[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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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/common.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/imgutils.h"
25 #include "avcodec.h"
26 #include "internal.h"
27
28 typedef struct DPXContext {
29     int big_endian;
30     int bits_per_component;
31     int descriptor;
32 } DPXContext;
33
34 static av_cold int encode_init(AVCodecContext *avctx)
35 {
36     DPXContext *s = avctx->priv_data;
37
38 #if FF_API_CODED_FRAME
39 FF_DISABLE_DEPRECATION_WARNINGS
40     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
41     avctx->coded_frame->key_frame = 1;
42 FF_ENABLE_DEPRECATION_WARNINGS
43 #endif
44
45     s->big_endian         = 1;
46     s->bits_per_component = 8;
47     s->descriptor         = 50; /* RGB */
48
49     switch (avctx->pix_fmt) {
50     case AV_PIX_FMT_RGB24:
51         break;
52     case AV_PIX_FMT_RGBA:
53         s->descriptor = 51; /* RGBA */
54         break;
55     case AV_PIX_FMT_RGB48LE:
56         s->big_endian = 0;
57         /* fall-through */
58     case AV_PIX_FMT_RGB48BE:
59         s->bits_per_component = avctx->bits_per_raw_sample ? avctx->bits_per_raw_sample : 16;
60         break;
61     default:
62         av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
63         return -1;
64     }
65
66     return 0;
67 }
68
69 #define write16(p, value) \
70 do { \
71     if (s->big_endian) AV_WB16(p, value); \
72     else               AV_WL16(p, value); \
73 } while(0)
74
75 #define write32(p, value) \
76 do { \
77     if (s->big_endian) AV_WB32(p, value); \
78     else               AV_WL32(p, value); \
79 } while(0)
80
81 static void encode_rgb48_10bit(AVCodecContext *avctx, const AVFrame *pic,
82                                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, AVPacket *pkt,
108                         const AVFrame *frame, int *got_packet)
109 {
110     DPXContext *s = avctx->priv_data;
111     int size, ret;
112     uint8_t *buf;
113
114 #define HEADER_SIZE 1664  /* DPX Generic header */
115     if (s->bits_per_component == 10)
116         size = avctx->height * avctx->width * 4;
117     else
118         size = av_image_get_buffer_size(avctx->pix_fmt,
119                                         avctx->width, avctx->height, 1);
120     if ((ret = ff_alloc_packet(pkt, size + HEADER_SIZE)) < 0) {
121         av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
122         return ret;
123     }
124     buf = pkt->data;
125
126     memset(buf, 0, HEADER_SIZE);
127
128     /* File information header */
129     write32(buf,       MKBETAG('S','D','P','X'));
130     write32(buf +   4, HEADER_SIZE);
131     memcpy (buf +   8, "V1.0", 4);
132     write32(buf +  20, 1); /* new image */
133     write32(buf +  24, HEADER_SIZE);
134     if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT))
135         memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
136     write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
137
138     /* Image information header */
139     write16(buf + 768, 0); /* orientation; left to right, top to bottom */
140     write16(buf + 770, 1); /* number of elements */
141     write32(buf + 772, avctx->width);
142     write32(buf + 776, avctx->height);
143     buf[800] = s->descriptor;
144     buf[801] = 2; /* linear transfer */
145     buf[802] = 2; /* linear colorimetric */
146     buf[803] = s->bits_per_component;
147     write16(buf + 804, s->bits_per_component == 10 ? 1 : 0); /* packing method */
148     write32(buf + 808, HEADER_SIZE); /* data offset */
149
150     /* Image source information header */
151     write32(buf + 1628, avctx->sample_aspect_ratio.num);
152     write32(buf + 1632, avctx->sample_aspect_ratio.den);
153
154     switch (s->bits_per_component) {
155     case 8:
156     case 16:
157         size = av_image_copy_to_buffer(buf + HEADER_SIZE,
158                                        pkt->size - HEADER_SIZE,
159                                        frame->data, frame->linesize,
160                                        avctx->pix_fmt,
161                                        avctx->width, avctx->height, 1);
162         if (size < 0)
163             return size;
164         break;
165     case 10:
166         encode_rgb48_10bit(avctx, frame, buf + HEADER_SIZE);
167         break;
168     default:
169         av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
170         return -1;
171     }
172
173     size += HEADER_SIZE;
174
175     write32(buf + 16, size); /* file size */
176
177     pkt->flags |= AV_PKT_FLAG_KEY;
178     *got_packet = 1;
179
180     return 0;
181 }
182
183 AVCodec ff_dpx_encoder = {
184     .name = "dpx",
185     .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
186     .type = AVMEDIA_TYPE_VIDEO,
187     .id   = AV_CODEC_ID_DPX,
188     .priv_data_size = sizeof(DPXContext),
189     .init   = encode_init,
190     .encode2 = encode_frame,
191     .pix_fmts = (const enum AVPixelFormat[]){
192         AV_PIX_FMT_RGB24,
193         AV_PIX_FMT_RGBA,
194         AV_PIX_FMT_RGB48LE,
195         AV_PIX_FMT_RGB48BE,
196         AV_PIX_FMT_NONE},
197 };