nuv.c
Go to the documentation of this file.
1 /*
2  * NuppelVideo decoder
3  * Copyright (c) 2006 Reimar Doeffinger
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 #include <stdio.h>
22 #include <stdlib.h>
23 
24 #include "libavutil/bswap.h"
25 #include "libavutil/lzo.h"
26 #include "libavutil/imgutils.h"
27 #include "avcodec.h"
28 #include "dsputil.h"
29 #include "rtjpeg.h"
30 
31 typedef struct {
34  int quality;
35  int width, height;
36  unsigned int decomp_size;
37  unsigned char* decomp_buf;
38  uint32_t lq[64], cq[64];
41 } NuvContext;
42 
43 static const uint8_t fallback_lquant[] = {
44  16, 11, 10, 16, 24, 40, 51, 61,
45  12, 12, 14, 19, 26, 58, 60, 55,
46  14, 13, 16, 24, 40, 57, 69, 56,
47  14, 17, 22, 29, 51, 87, 80, 62,
48  18, 22, 37, 56, 68, 109, 103, 77,
49  24, 35, 55, 64, 81, 104, 113, 92,
50  49, 64, 78, 87, 103, 121, 120, 101,
51  72, 92, 95, 98, 112, 100, 103, 99
52 };
53 
54 static const uint8_t fallback_cquant[] = {
55  17, 18, 24, 47, 99, 99, 99, 99,
56  18, 21, 26, 66, 99, 99, 99, 99,
57  24, 26, 56, 99, 99, 99, 99, 99,
58  47, 66, 99, 99, 99, 99, 99, 99,
59  99, 99, 99, 99, 99, 99, 99, 99,
60  99, 99, 99, 99, 99, 99, 99, 99,
61  99, 99, 99, 99, 99, 99, 99, 99,
62  99, 99, 99, 99, 99, 99, 99, 99
63 };
64 
72 static void copy_frame(AVFrame *f, const uint8_t *src,
73  int width, int height) {
74  AVPicture pic;
75  avpicture_fill(&pic, src, PIX_FMT_YUV420P, width, height);
76  av_picture_copy((AVPicture *)f, &pic, PIX_FMT_YUV420P, width, height);
77 }
78 
82 static int get_quant(AVCodecContext *avctx, NuvContext *c,
83  const uint8_t *buf, int size) {
84  int i;
85  if (size < 2 * 64 * 4) {
86  av_log(avctx, AV_LOG_ERROR, "insufficient rtjpeg quant data\n");
87  return AVERROR_INVALIDDATA;
88  }
89  for (i = 0; i < 64; i++, buf += 4)
90  c->lq[i] = AV_RL32(buf);
91  for (i = 0; i < 64; i++, buf += 4)
92  c->cq[i] = AV_RL32(buf);
93  return 0;
94 }
95 
99 static void get_quant_quality(NuvContext *c, int quality) {
100  int i;
101  quality = FFMAX(quality, 1);
102  for (i = 0; i < 64; i++) {
103  c->lq[i] = (fallback_lquant[i] << 7) / quality;
104  c->cq[i] = (fallback_cquant[i] << 7) / quality;
105  }
106 }
107 
108 static int codec_reinit(AVCodecContext *avctx, int width, int height, int quality) {
109  NuvContext *c = avctx->priv_data;
110  int ret;
111 
112  width = FFALIGN(width, 2);
113  height = FFALIGN(height, 2);
114  if (quality >= 0)
115  get_quant_quality(c, quality);
116  if (width != c->width || height != c->height) {
117  void *ptr;
118  if ((ret = av_image_check_size(height, width, 0, avctx)) < 0)
119  return ret;
120  avctx->width = c->width = width;
121  avctx->height = c->height = height;
122  ptr = av_fast_realloc(c->decomp_buf, &c->decomp_size,
123  c->height * c->width * 3 / 2 +
126  if (!ptr) {
127  av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
128  return AVERROR(ENOMEM);
129  } else
130  c->decomp_buf = ptr;
131  rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq);
132  if (c->pic.data[0])
133  avctx->release_buffer(avctx, &c->pic);
134  } else if (quality != c->quality)
135  rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq);
136  return 0;
137 }
138 
139 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
140  AVPacket *avpkt) {
141  const uint8_t *buf = avpkt->data;
142  int buf_size = avpkt->size;
143  NuvContext *c = avctx->priv_data;
144  AVFrame *picture = data;
145  int orig_size = buf_size;
146  int keyframe;
147  int result;
148  int ret;
149  enum {NUV_UNCOMPRESSED = '0', NUV_RTJPEG = '1',
150  NUV_RTJPEG_IN_LZO = '2', NUV_LZO = '3',
151  NUV_BLACK = 'N', NUV_COPY_LAST = 'L'} comptype;
152 
153  if (buf_size < 12) {
154  av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
155  return AVERROR_INVALIDDATA;
156  }
157 
158  // codec data (rtjpeg quant tables)
159  if (buf[0] == 'D' && buf[1] == 'R') {
160  int ret;
161  // skip rest of the frameheader.
162  buf = &buf[12];
163  buf_size -= 12;
164  ret = get_quant(avctx, c, buf, buf_size);
165  if (ret < 0)
166  return ret;
167  rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq);
168  return orig_size;
169  }
170 
171  if (buf[0] != 'V' || buf_size < 12) {
172  av_log(avctx, AV_LOG_ERROR, "not a nuv video frame\n");
173  return AVERROR_INVALIDDATA;
174  }
175  comptype = buf[1];
176  switch (comptype) {
177  case NUV_RTJPEG_IN_LZO:
178  case NUV_RTJPEG:
179  keyframe = !buf[2]; break;
180  case NUV_COPY_LAST:
181  keyframe = 0; break;
182  default:
183  keyframe = 1; break;
184  }
185  // skip rest of the frameheader.
186  buf = &buf[12];
187  buf_size -= 12;
188  if (comptype == NUV_RTJPEG_IN_LZO || comptype == NUV_LZO) {
189  int outlen = c->decomp_size - FF_INPUT_BUFFER_PADDING_SIZE;
190  int inlen = buf_size;
191  if (av_lzo1x_decode(c->decomp_buf, &outlen, buf, &inlen)) {
192  av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
193  return AVERROR_INVALIDDATA;
194  }
195  buf = c->decomp_buf;
196  buf_size = outlen;
197  }
198  if (c->codec_frameheader) {
199  int w, h, q;
200  if (buf_size < RTJPEG_HEADER_SIZE || buf[4] != RTJPEG_HEADER_SIZE ||
201  buf[5] != RTJPEG_FILE_VERSION) {
202  av_log(avctx, AV_LOG_ERROR, "invalid nuv video frame\n");
203  return AVERROR_INVALIDDATA;
204  }
205  w = AV_RL16(&buf[6]);
206  h = AV_RL16(&buf[8]);
207  q = buf[10];
208  if ((result = codec_reinit(avctx, w, h, q)) < 0)
209  return result;
210  if (comptype == NUV_RTJPEG_IN_LZO || comptype == NUV_LZO)
211  buf = c->decomp_buf;
212  buf = &buf[RTJPEG_HEADER_SIZE];
213  buf_size -= RTJPEG_HEADER_SIZE;
214  }
215 
216  if (keyframe && c->pic.data[0])
217  avctx->release_buffer(avctx, &c->pic);
218  c->pic.reference = 3;
221  result = avctx->reget_buffer(avctx, &c->pic);
222  if (result < 0) {
223  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
224  return result;
225  }
226 
228  c->pic.key_frame = keyframe;
229  // decompress/copy/whatever data
230  switch (comptype) {
231  case NUV_LZO:
232  case NUV_UNCOMPRESSED: {
233  int height = c->height;
234  if (buf_size < c->width * height * 3 / 2) {
235  av_log(avctx, AV_LOG_ERROR, "uncompressed frame too short\n");
236  height = buf_size / c->width / 3 * 2;
237  }
238  copy_frame(&c->pic, buf, c->width, height);
239  break;
240  }
241  case NUV_RTJPEG_IN_LZO:
242  case NUV_RTJPEG: {
243  ret = rtjpeg_decode_frame_yuv420(&c->rtj, &c->pic, buf, buf_size);
244  if (ret < 0)
245  return ret;
246  break;
247  }
248  case NUV_BLACK: {
249  memset(c->pic.data[0], 0, c->width * c->height);
250  memset(c->pic.data[1], 128, c->width * c->height / 4);
251  memset(c->pic.data[2], 128, c->width * c->height / 4);
252  break;
253  }
254  case NUV_COPY_LAST: {
255  /* nothing more to do here */
256  break;
257  }
258  default:
259  av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
260  return AVERROR_INVALIDDATA;
261  }
262 
263  *picture = c->pic;
264  *data_size = sizeof(AVFrame);
265  return orig_size;
266 }
267 
268 static av_cold int decode_init(AVCodecContext *avctx) {
269  NuvContext *c = avctx->priv_data;
270  int ret;
271 
272  avctx->pix_fmt = PIX_FMT_YUV420P;
273  c->pic.data[0] = NULL;
274  c->decomp_buf = NULL;
275  c->quality = -1;
276  c->width = 0;
277  c->height = 0;
278  c->codec_frameheader = avctx->codec_tag == MKTAG('R', 'J', 'P', 'G');
279  if (avctx->extradata_size)
280  get_quant(avctx, c, avctx->extradata, avctx->extradata_size);
281  dsputil_init(&c->dsp, avctx);
282  if ((ret = codec_reinit(avctx, avctx->width, avctx->height, -1)) < 0)
283  return ret;
284  return 0;
285 }
286 
287 static av_cold int decode_end(AVCodecContext *avctx) {
288  NuvContext *c = avctx->priv_data;
289  av_freep(&c->decomp_buf);
290  if (c->pic.data[0])
291  avctx->release_buffer(avctx, &c->pic);
292  return 0;
293 }
294 
296  .name = "nuv",
297  .type = AVMEDIA_TYPE_VIDEO,
298  .id = CODEC_ID_NUV,
299  .priv_data_size = sizeof(NuvContext),
300  .init = decode_init,
301  .close = decode_end,
302  .decode = decode_frame,
303  .capabilities = CODEC_CAP_DR1,
304  .long_name = NULL_IF_CONFIG_SMALL("NuppelVideo/RTJPEG"),
305 };
306 
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
static av_cold int decode_end(AVCodecContext *avctx)
Definition: nuv.c:287
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int width
Definition: nuv.c:35
int size
#define FF_BUFFER_HINTS_VALID
Definition: avcodec.h:880
Audio Video Frame.
Definition: avcodec.h:985
int height
Definition: nuv.c:35
int rtjpeg_decode_frame_yuv420(RTJpegContext *c, AVFrame *f, const uint8_t *buf, int buf_size)
decode one rtjpeg YUV420 frame
Definition: rtjpeg.c:107
int buffer_hints
codec suggestion on buffer type if != 0
Definition: avcodec.h:1195
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:1737
misc image utilities
#define RTJPEG_FILE_VERSION
Definition: rtjpeg.h:28
AVFrame pic
Definition: nuv.c:32
void rtjpeg_decode_init(RTJpegContext *c, DSPContext *dsp, int width, int height, const uint32_t *lquant, const uint32_t *cquant)
initialize an RTJpegContext, may be called multiple times
Definition: rtjpeg.c:157
int size
Definition: avcodec.h:909
int width
Definition: rotozoom.c:164
static int get_quant(AVCodecContext *avctx, NuvContext *c, const uint8_t *buf, int size)
extract quantization tables from codec data into our context
Definition: nuv.c:82
DSPContext dsp
Definition: nuv.c:40
int codec_frameheader
Definition: nuv.c:33
static void get_quant_quality(NuvContext *c, int quality)
set quantization tables from a quality value
Definition: nuv.c:99
static const uint8_t fallback_lquant[]
Definition: nuv.c:43
four components are given, that's all.
Definition: avcodec.h:3367
AVCodec.
Definition: avcodec.h:3189
#define FFALIGN(x, a)
Definition: common.h:60
int(* reget_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of a frame to get cr buffer for it.
Definition: avcodec.h:2336
int quality
Definition: nuv.c:34
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:147
Definition: nuv.c:31
void av_picture_copy(AVPicture *dst, const AVPicture *src, enum PixelFormat pix_fmt, int width, int height)
Copy image src to dst.
Definition: imgconvert.c:664
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:64
#define av_cold
Definition: attributes.h:71
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1387
const char data[16]
Definition: mxf.c:60
uint8_t * data
Definition: avcodec.h:908
av_cold void dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2789
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:719
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1074
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
AV_RL32
Definition: bytestream.h:89
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
#define FFMAX(a, b)
Definition: common.h:53
static av_cold int decode_init(AVCodecContext *avctx)
Definition: nuv.c:268
AVCodec ff_nuv_decoder
Definition: nuv.c:295
unsigned char * decomp_buf
Definition: nuv.c:37
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:213
static AVFrame * picture
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1029
#define f(n)
Definition: regs.h:33
int width
picture width / height.
Definition: avcodec.h:1408
struct AVFrame AVFrame
Audio Video Frame.
int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen)
Decodes LZO 1x compressed data.
Definition: lzo.c:181
#define FF_BUFFER_HINTS_REUSABLE
Definition: avcodec.h:883
NULL
Definition: eval.c:50
external API header
main external API structure.
Definition: avcodec.h:1329
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:327
static void copy_frame(AVFrame *f, const uint8_t *src, int width, int height)
copy frame data from buffer to AVFrame, handling stride.
Definition: nuv.c:72
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1590
int extradata_size
Definition: avcodec.h:1388
#define FF_BUFFER_HINTS_PRESERVE
Definition: avcodec.h:882
int avpicture_fill(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt, int width, int height)
Fill in the AVPicture fields.
Definition: imgconvert.c:429
byte swapping routines
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:497
#define FF_BUFFER_HINTS_READABLE
Definition: avcodec.h:881
unsigned int decomp_size
Definition: nuv.c:36
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
static const uint8_t fallback_cquant[]
Definition: nuv.c:54
int height
Definition: gxfenc.c:73
AV_WL32 AV_WL24 AV_RL16
Definition: bytestream.h:89
DSP utils.
void * priv_data
Definition: avcodec.h:1531
static int codec_reinit(AVCodecContext *avctx, int width, int height, int quality)
Definition: nuv.c:108
uint32_t lq[64]
Definition: nuv.c:38
#define RTJPEG_HEADER_SIZE
Definition: rtjpeg.h:29
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given block if it is not large enough, otherwise do nothing.
Definition: utils.c:55
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1022
#define lq(base, off, reg)
Definition: mmi.h:62
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
Definition: nuv.c:139
RTJpegContext rtj
Definition: nuv.c:39
#define MKTAG(a, b, c, d)
Definition: common.h:231
uint32_t cq[64]
Definition: nuv.c:38
Predicted.
Definition: avutil.h:297
DSPContext.
Definition: dsputil.h:226