3 * Copyright (c) 2006 Reimar Doeffinger
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 unsigned int decomp_size
;
34 unsigned char* decomp_buf
;
35 uint32_t lq
[64], cq
[64];
41 * \brief copy frame data from buffer to AVFrame, handling stride.
42 * \param f destination AVFrame
43 * \param src source buffer, does not use any line-stride
44 * \param width width of the video frame
45 * \param height height of the video frame
47 static void copy_frame(AVFrame
*f
, uint8_t *src
,
48 int width
, int height
) {
50 avpicture_fill(&pic
, src
, PIX_FMT_YUV420P
, width
, height
);
51 img_copy((AVPicture
*)f
, &pic
, PIX_FMT_YUV420P
, width
, height
);
55 * \brief extract quantization tables from codec data into our context
57 static int get_quant(AVCodecContext
*avctx
, NuvContext
*c
,
58 uint8_t *buf
, int size
) {
60 if (size
< 2 * 64 * 4) {
61 av_log(avctx
, AV_LOG_ERROR
, "insufficient rtjpeg quant data\n");
64 for (i
= 0; i
< 64; i
++, buf
+= 4)
65 c
->lq
[i
] = LE_32(buf
);
66 for (i
= 0; i
< 64; i
++, buf
+= 4)
67 c
->cq
[i
] = LE_32(buf
);
71 static int decode_frame(AVCodecContext
*avctx
, void *data
, int *data_size
,
72 uint8_t *buf
, int buf_size
) {
73 NuvContext
*c
= (NuvContext
*)avctx
->priv_data
;
74 AVFrame
*picture
= data
;
75 int orig_size
= buf_size
;
76 enum {NUV_UNCOMPRESSED
= '0', NUV_RTJPEG
= '1',
77 NUV_RTJPEG_IN_LZO
= '2', NUV_LZO
= '3',
78 NUV_BLACK
= 'N', NUV_COPY_LAST
= 'L'} comptype
;
81 av_log(avctx
, AV_LOG_ERROR
, "coded frame too small\n");
86 avctx
->release_buffer(avctx
, &c
->pic
);
88 c
->pic
.buffer_hints
= FF_BUFFER_HINTS_VALID
| FF_BUFFER_HINTS_READABLE
|
89 FF_BUFFER_HINTS_PRESERVE
| FF_BUFFER_HINTS_REUSABLE
;
90 if (avctx
->get_buffer(avctx
, &c
->pic
) < 0) {
91 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
95 // codec data (rtjpeg quant tables)
96 if (buf
[0] == 'D' && buf
[1] == 'R') {
98 // skip rest of the frameheader.
101 ret
= get_quant(avctx
, c
, buf
, buf_size
);
104 rtjpeg_decode_init(&c
->rtj
, &c
->dsp
, c
->width
, c
->height
, c
->lq
, c
->cq
);
108 if (buf
[0] != 'V' || buf_size
< 12) {
109 av_log(avctx
, AV_LOG_ERROR
, "not a nuv video frame\n");
113 // skip rest of the frameheader.
117 c
->pic
.pict_type
= FF_I_TYPE
;
118 c
->pic
.key_frame
= 1;
119 // decompress/copy/whatever data
121 case NUV_UNCOMPRESSED
: {
122 int height
= c
->height
;
123 if (buf_size
< c
->width
* height
* 3 / 2) {
124 av_log(avctx
, AV_LOG_ERROR
, "uncompressed frame too short\n");
125 height
= buf_size
/ c
->width
/ 3 * 2;
127 copy_frame(&c
->pic
, buf
, c
->width
, height
);
131 rtjpeg_decode_frame_yuv420(&c
->rtj
, &c
->pic
, buf
, buf_size
);
134 case NUV_RTJPEG_IN_LZO
: {
135 int outlen
= c
->decomp_size
, inlen
= buf_size
;
136 if (lzo1x_decode(c
->decomp_buf
, &outlen
, buf
, &inlen
))
137 av_log(avctx
, AV_LOG_ERROR
, "error during lzo decompression\n");
138 rtjpeg_decode_frame_yuv420(&c
->rtj
, &c
->pic
, c
->decomp_buf
, c
->decomp_size
);
142 int outlen
= c
->decomp_size
, inlen
= buf_size
;
143 if (lzo1x_decode(c
->decomp_buf
, &outlen
, buf
, &inlen
))
144 av_log(avctx
, AV_LOG_ERROR
, "error during lzo decompression\n");
145 copy_frame(&c
->pic
, c
->decomp_buf
, c
->width
, c
->height
);
149 memset(c
->pic
.data
[0], 0, c
->width
* c
->height
);
150 memset(c
->pic
.data
[1], 128, c
->width
* c
->height
/ 4);
151 memset(c
->pic
.data
[2], 128, c
->width
* c
->height
/ 4);
154 case NUV_COPY_LAST
: {
155 c
->pic
.pict_type
= FF_P_TYPE
;
156 c
->pic
.key_frame
= 0;
157 /* nothing more to do here */
161 av_log(avctx
, AV_LOG_ERROR
, "unknown compression\n");
166 *data_size
= sizeof(AVFrame
);
170 static int decode_init(AVCodecContext
*avctx
) {
171 NuvContext
*c
= (NuvContext
*)avctx
->priv_data
;
172 avctx
->width
= (avctx
->width
+ 1) & ~1;
173 avctx
->height
= (avctx
->height
+ 1) & ~1;
174 if (avcodec_check_dimensions(avctx
, avctx
->height
, avctx
->width
) < 0) {
177 avctx
->has_b_frames
= 0;
178 avctx
->pix_fmt
= PIX_FMT_YUV420P
;
179 c
->pic
.data
[0] = NULL
;
180 c
->width
= avctx
->width
;
181 c
->height
= avctx
->height
;
182 c
->decomp_size
= c
->height
* c
->width
* 3 / 2;
183 c
->decomp_buf
= av_malloc(c
->decomp_size
+ LZO_OUTPUT_PADDING
);
184 if (!c
->decomp_buf
) {
185 av_log(avctx
, AV_LOG_ERROR
, "Can't allocate decompression buffer.\n");
188 dsputil_init(&c
->dsp
, avctx
);
189 if (avctx
->extradata_size
)
190 get_quant(avctx
, c
, avctx
->extradata
, avctx
->extradata_size
);
191 rtjpeg_decode_init(&c
->rtj
, &c
->dsp
, c
->width
, c
->height
, c
->lq
, c
->cq
);
195 static int decode_end(AVCodecContext
*avctx
) {
196 NuvContext
*c
= (NuvContext
*)avctx
->priv_data
;
197 av_freep(&c
->decomp_buf
);
199 avctx
->release_buffer(avctx
, &c
->pic
);
203 AVCodec nuv_decoder
= {