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 = AV_PIX_FMT_PAL8;
44  ff_dsputil_init(&s->dsp, avctx);
45  return 0;
46 }
47 
51 static inline void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
52 {
53  int i, j, v[2];
54 
55  switch (get_bits(gb, 2)) {
56  case 1:
57  v[0] = get_bits(gb, 8);
58  for (j = 0; j < 2; j++)
59  memset(dst + j*linesize, v[0], 2);
60  break;
61  case 2:
62  v[0] = get_bits(gb, 8);
63  v[1] = get_bits(gb, 8);
64  for (j = 0; j < 2; j++)
65  for (i = 0; i < 2; i++)
66  dst[j*linesize + i] = v[get_bits1(gb)];
67  break;
68  case 3:
69  for (j = 0; j < 2; j++)
70  for (i = 0; i < 2; i++)
71  dst[j*linesize + i] = get_bits(gb, 8);
72  }
73 }
74 
78 static inline void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
79 {
80  int i, j, v[2];
81 
82  switch (get_bits(gb, 2)) {
83  case 1:
84  v[0] = get_bits(gb, 8);
85  for (j = 0; j < 4; j++)
86  memset(dst + j*linesize, v[0], 4);
87  break;
88  case 2:
89  v[0] = get_bits(gb, 8);
90  v[1] = get_bits(gb, 8);
91  for (j = 2; j >= 0; j -= 2) {
92  for (i = 0; i < 4; i++)
93  dst[j*linesize + i] = v[get_bits1(gb)];
94  for (i = 0; i < 4; i++)
95  dst[(j+1)*linesize + i] = v[get_bits1(gb)];
96  }
97  break;
98  case 3:
99  for (j = 0; j < 4; j += 2)
100  for (i = 0; i < 4; i += 2)
101  decode2x2(gb, dst + j*linesize + i, linesize);
102  }
103 }
104 
108 static inline void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize, DSPContext *dsp)
109 {
110  int i, j, v[2];
111 
112  switch (get_bits(gb, 2)) {
113  case 1:
114  v[0] = get_bits(gb, 8);
115  dsp->fill_block_tab[1](dst, v[0], linesize, 8);
116  break;
117  case 2:
118  v[0] = get_bits(gb, 8);
119  v[1] = get_bits(gb, 8);
120  for (j = 7; j >= 0; j--)
121  for (i = 0; i < 8; i++)
122  dst[j*linesize + i] = v[get_bits1(gb)];
123  break;
124  case 3:
125  for (j = 0; j < 8; j += 4)
126  for (i = 0; i < 8; i += 4)
127  decode4x4(gb, dst + j*linesize + i, linesize);
128  }
129 }
130 
131 static int decode_frame(AVCodecContext *avctx,
132  void *data, int *got_frame,
133  AVPacket *avpkt)
134 {
135  JvContext *s = avctx->priv_data;
136  int buf_size = avpkt->size;
137  const uint8_t *buf = avpkt->data;
138  const uint8_t *buf_end = buf + buf_size;
139  int video_size, video_type, i, j;
140 
141  video_size = AV_RL32(buf);
142  video_type = buf[4];
143  buf += 5;
144 
145  if (video_size) {
146  if (avctx->reget_buffer(avctx, &s->frame) < 0) {
147  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
148  return -1;
149  }
150 
151  if (video_type == 0 || video_type == 1) {
152  GetBitContext gb;
153  init_get_bits(&gb, buf, 8 * FFMIN(video_size, buf_end - buf));
154 
155  for (j = 0; j < avctx->height; j += 8)
156  for (i = 0; i < avctx->width; i += 8)
157  decode8x8(&gb, s->frame.data[0] + j*s->frame.linesize[0] + i,
158  s->frame.linesize[0], &s->dsp);
159 
160  buf += video_size;
161  } else if (video_type == 2) {
162  if (buf + 1 <= buf_end) {
163  int v = *buf++;
164  for (j = 0; j < avctx->height; j++)
165  memset(s->frame.data[0] + j*s->frame.linesize[0], v, avctx->width);
166  }
167  } else {
168  av_log(avctx, AV_LOG_WARNING, "unsupported frame type %i\n", video_type);
169  return AVERROR_INVALIDDATA;
170  }
171  }
172 
173  if (buf < buf_end) {
174  for (i = 0; i < AVPALETTE_COUNT && buf + 3 <= buf_end; i++) {
175  s->palette[i] = AV_RB24(buf) << 2;
176  buf += 3;
177  }
178  s->palette_has_changed = 1;
179  }
180 
181  if (video_size) {
182  s->frame.key_frame = 1;
185  s->palette_has_changed = 0;
186  memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
187 
188  *got_frame = 1;
189  *(AVFrame*)data = s->frame;
190  }
191 
192  return buf_size;
193 }
194 
196 {
197  JvContext *s = avctx->priv_data;
198 
199  if(s->frame.data[0])
200  avctx->release_buffer(avctx, &s->frame);
201 
202  return 0;
203 }
204 
206  .name = "jv",
207  .long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV video"),
208  .type = AVMEDIA_TYPE_VIDEO,
209  .id = AV_CODEC_ID_JV,
210  .priv_data_size = sizeof(JvContext),
211  .init = decode_init,
212  .close = decode_close,
213  .decode = decode_frame,
214  .capabilities = CODEC_CAP_DR1,
215 };
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2656
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
int size
Definition: avcodec.h:916
#define AV_RB24
Definition: intreadwrite.h:64
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
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:2960
int(* reget_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of a frame to get cr buffer for it.
Definition: avcodec.h:2273
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
uint8_t
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
uint32_t palette[AVPALETTE_COUNT]
Definition: jvdec.c:36
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
bitstream reader API header.
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
AVCodec ff_jv_decoder
Definition: jvdec.c:205
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: jvdec.c:131
static void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize, DSPContext *dsp)
Decode 8x8 block.
Definition: jvdec.c:108
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
int width
picture width / height.
Definition: avcodec.h:1508
#define AV_RL32
Definition: intreadwrite.h:146
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:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
op_fill_func fill_block_tab[2]
Definition: dsputil.h:489
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:372
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: avcodec.h:1246
DSPContext dsp
Definition: jvdec.c:34
static av_cold int decode_close(AVCodecContext *avctx)
Definition: jvdec.c:195
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
static void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
Decode 2x2 block.
Definition: jvdec.c:51
static void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
Decode 4x4 block.
Definition: jvdec.c:78
DSP utils.
void * priv_data
Definition: avcodec.h:1382
static int64_t video_size
Definition: avconv.c:87
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
AVFrame frame
Definition: jvdec.c:35
This structure stores compressed data.
Definition: avcodec.h:898
DSPContext.
Definition: dsputil.h:194