jvdec.c
Go to the documentation of this file.
1 /*
2  * Bitmap Brothers JV video decoder
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 
28 #include "avcodec.h"
29 #include "dsputil.h"
30 #include "get_bits.h"
31 #include "libavutil/intreadwrite.h"
32 
33 typedef struct JvContext {
38 } JvContext;
39 
41 {
42  JvContext *s = avctx->priv_data;
43  avctx->pix_fmt = PIX_FMT_PAL8;
44  dsputil_init(&s->dsp, avctx);
45 
46  if (!avctx->width || !avctx->height ||
47  (avctx->width & 7) || (avctx->height & 7)) {
48  av_log(avctx, AV_LOG_ERROR, "Invalid video dimensions: %dx%d\n",
49  avctx->width, avctx->height);
50  return AVERROR(EINVAL);
51  }
52 
53  return 0;
54 }
55 
59 static inline void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
60 {
61  int i, j, v[2];
62 
63  switch (get_bits(gb, 2)) {
64  case 1:
65  v[0] = get_bits(gb, 8);
66  for (j = 0; j < 2; j++)
67  memset(dst + j*linesize, v[0], 2);
68  break;
69  case 2:
70  v[0] = get_bits(gb, 8);
71  v[1] = get_bits(gb, 8);
72  for (j = 0; j < 2; j++)
73  for (i = 0; i < 2; i++)
74  dst[j*linesize + i] = v[get_bits1(gb)];
75  break;
76  case 3:
77  for (j = 0; j < 2; j++)
78  for (i = 0; i < 2; i++)
79  dst[j*linesize + i] = get_bits(gb, 8);
80  }
81 }
82 
86 static inline void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
87 {
88  int i, j, v[2];
89 
90  switch (get_bits(gb, 2)) {
91  case 1:
92  v[0] = get_bits(gb, 8);
93  for (j = 0; j < 4; j++)
94  memset(dst + j*linesize, v[0], 4);
95  break;
96  case 2:
97  v[0] = get_bits(gb, 8);
98  v[1] = get_bits(gb, 8);
99  for (j = 2; j >= 0; j -= 2) {
100  for (i = 0; i < 4; i++)
101  dst[j*linesize + i] = v[get_bits1(gb)];
102  for (i = 0; i < 4; i++)
103  dst[(j+1)*linesize + i] = v[get_bits1(gb)];
104  }
105  break;
106  case 3:
107  for (j = 0; j < 4; j += 2)
108  for (i = 0; i < 4; i += 2)
109  decode2x2(gb, dst + j*linesize + i, linesize);
110  }
111 }
112 
116 static inline void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize, DSPContext *dsp)
117 {
118  int i, j, v[2];
119 
120  switch (get_bits(gb, 2)) {
121  case 1:
122  v[0] = get_bits(gb, 8);
123  dsp->fill_block_tab[1](dst, v[0], linesize, 8);
124  break;
125  case 2:
126  v[0] = get_bits(gb, 8);
127  v[1] = get_bits(gb, 8);
128  for (j = 7; j >= 0; j--)
129  for (i = 0; i < 8; i++)
130  dst[j*linesize + i] = v[get_bits1(gb)];
131  break;
132  case 3:
133  for (j = 0; j < 8; j += 4)
134  for (i = 0; i < 8; i += 4)
135  decode4x4(gb, dst + j*linesize + i, linesize);
136  }
137 }
138 
139 static int decode_frame(AVCodecContext *avctx,
140  void *data, int *data_size,
141  AVPacket *avpkt)
142 {
143  JvContext *s = avctx->priv_data;
144  int buf_size = avpkt->size;
145  const uint8_t *buf = avpkt->data;
146  const uint8_t *buf_end = buf + buf_size;
147  int video_size, video_type, i, j;
148 
149  video_size = AV_RL32(buf);
150  video_type = buf[4];
151  buf += 5;
152 
153  if (video_size) {
154  if (avctx->reget_buffer(avctx, &s->frame) < 0) {
155  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
156  return -1;
157  }
158 
159  if (video_type == 0 || video_type == 1) {
160  GetBitContext gb;
161  init_get_bits(&gb, buf, 8 * FFMIN(video_size, buf_end - buf));
162 
163  for (j = 0; j < avctx->height; j += 8)
164  for (i = 0; i < avctx->width; i += 8)
165  decode8x8(&gb, s->frame.data[0] + j*s->frame.linesize[0] + i,
166  s->frame.linesize[0], &s->dsp);
167 
168  buf += video_size;
169  } else if (video_type == 2) {
170  if (buf + 1 <= buf_end) {
171  int v = *buf++;
172  for (j = 0; j < avctx->height; j++)
173  memset(s->frame.data[0] + j*s->frame.linesize[0], v, avctx->width);
174  }
175  } else {
176  av_log(avctx, AV_LOG_WARNING, "unsupported frame type %i\n", video_type);
177  return AVERROR_INVALIDDATA;
178  }
179  }
180 
181  if (buf < buf_end) {
182  for (i = 0; i < AVPALETTE_COUNT && buf + 3 <= buf_end; i++) {
183  s->palette[i] = AV_RB24(buf) << 2;
184  buf += 3;
185  }
186  s->palette_has_changed = 1;
187  }
188 
189  if (video_size) {
190  s->frame.key_frame = 1;
193  s->palette_has_changed = 0;
194  memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
195 
196  *data_size = sizeof(AVFrame);
197  *(AVFrame*)data = s->frame;
198  }
199 
200  return buf_size;
201 }
202 
204 {
205  JvContext *s = avctx->priv_data;
206 
207  if(s->frame.data[0])
208  avctx->release_buffer(avctx, &s->frame);
209 
210  return 0;
211 }
212 
214  .name = "jv",
215  .long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV video"),
216  .type = AVMEDIA_TYPE_VIDEO,
217  .id = CODEC_ID_JV,
218  .priv_data_size = sizeof(JvContext),
219  .init = decode_init,
220  .close = decode_close,
221  .decode = decode_frame,
222  .capabilities = CODEC_CAP_DR1,
223 };
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
Audio Video Frame.
Definition: avcodec.h:985
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:1737
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:117
int size
Definition: avcodec.h:909
int palette_has_changed
Definition: jvdec.c:37
struct JvContext JvContext
static av_cold int decode_init(AVCodecContext *avctx)
Definition: jvdec.c:40
AVCodec.
Definition: avcodec.h:3189
#define v(n)
Definition: regs.h:34
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
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
#define av_cold
Definition: attributes.h:71
uint32_t palette[AVPALETTE_COUNT]
Definition: jvdec.c:36
const char data[16]
Definition: mxf.c:60
uint8_t * data
Definition: avcodec.h:908
bitstream reader API header.
av_cold void dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2789
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
static DSPContext dsp
Definition: atrac3.c:135
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:719
#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
AVCodec ff_jv_decoder
Definition: jvdec.c:213
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
static void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize, DSPContext *dsp)
Decode 8x8 block.
Definition: jvdec.c:116
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1029
#define FFMIN(a, b)
Definition: common.h:55
int width
picture width / height.
Definition: avcodec.h:1408
struct AVFrame AVFrame
Audio Video Frame.
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
Definition: jvdec.c:139
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1329
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:327
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
op_fill_func fill_block_tab[2]
Definition: dsputil.h:583
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:354
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: avcodec.h:1188
#define AVPALETTE_COUNT
Definition: avcodec.h:3373
DSPContext dsp
Definition: jvdec.c:34
static av_cold int decode_close(AVCodecContext *avctx)
Definition: jvdec.c:203
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
AV_WL32 AV_WL24 AV_WL16 AV_WB32 AV_RB24
Definition: bytestream.h:89
static void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
Decode 2x2 block.
Definition: jvdec.c:59
static void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
Decode 4x4 block.
Definition: jvdec.c:86
#define AVPALETTE_SIZE
Definition: avcodec.h:3372
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:75
DSP utils.
void * priv_data
Definition: avcodec.h:1531
static int64_t video_size
Definition: avconv.c:132
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1022
AVFrame frame
Definition: jvdec.c:35
DSPContext.
Definition: dsputil.h:226