Libav
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 "libavutil/intreadwrite.h"
29 
30 #include "avcodec.h"
31 #include "dsputil.h"
32 #include "get_bits.h"
33 #include "internal.h"
34 
35 typedef struct JvContext {
40 } JvContext;
41 
43 {
44  JvContext *s = avctx->priv_data;
45 
46  s->frame = av_frame_alloc();
47  if (!s->frame)
48  return AVERROR(ENOMEM);
49 
50  avctx->pix_fmt = AV_PIX_FMT_PAL8;
51  ff_dsputil_init(&s->dsp, avctx);
52  return 0;
53 }
54 
58 static inline void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
59 {
60  int i, j, v[2];
61 
62  switch (get_bits(gb, 2)) {
63  case 1:
64  v[0] = get_bits(gb, 8);
65  for (j = 0; j < 2; j++)
66  memset(dst + j * linesize, v[0], 2);
67  break;
68  case 2:
69  v[0] = get_bits(gb, 8);
70  v[1] = get_bits(gb, 8);
71  for (j = 0; j < 2; j++)
72  for (i = 0; i < 2; i++)
73  dst[j * linesize + i] = v[get_bits1(gb)];
74  break;
75  case 3:
76  for (j = 0; j < 2; j++)
77  for (i = 0; i < 2; i++)
78  dst[j * linesize + i] = get_bits(gb, 8);
79  }
80 }
81 
85 static inline void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
86 {
87  int i, j, v[2];
88 
89  switch (get_bits(gb, 2)) {
90  case 1:
91  v[0] = get_bits(gb, 8);
92  for (j = 0; j < 4; j++)
93  memset(dst + j * linesize, v[0], 4);
94  break;
95  case 2:
96  v[0] = get_bits(gb, 8);
97  v[1] = get_bits(gb, 8);
98  for (j = 2; j >= 0; j -= 2) {
99  for (i = 0; i < 4; i++)
100  dst[j * linesize + i] = v[get_bits1(gb)];
101  for (i = 0; i < 4; i++)
102  dst[(j + 1) * linesize + i] = v[get_bits1(gb)];
103  }
104  break;
105  case 3:
106  for (j = 0; j < 4; j += 2)
107  for (i = 0; i < 4; i += 2)
108  decode2x2(gb, dst + j * linesize + i, linesize);
109  }
110 }
111 
115 static inline void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize,
116  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, void *data, int *got_frame,
140  AVPacket *avpkt)
141 {
142  JvContext *s = avctx->priv_data;
143  int buf_size = avpkt->size;
144  const uint8_t *buf = avpkt->data;
145  const uint8_t *buf_end = buf + buf_size;
146  int video_size, video_type, i, j, ret;
147 
148  video_size = AV_RL32(buf);
149  video_type = buf[4];
150  buf += 5;
151 
152  if (video_size) {
153  if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) {
154  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
155  return ret;
156  }
157 
158  if (video_type == 0 || video_type == 1) {
159  GetBitContext gb;
160  init_get_bits(&gb, buf, 8 * FFMIN(video_size, buf_end - buf));
161 
162  for (j = 0; j < avctx->height; j += 8)
163  for (i = 0; i < avctx->width; i += 8)
164  decode8x8(&gb,
165  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],
174  v, avctx->width);
175  }
176  } else {
177  av_log(avctx, AV_LOG_WARNING,
178  "unsupported frame type %i\n", video_type);
179  return AVERROR_INVALIDDATA;
180  }
181  }
182 
183  if (buf < buf_end) {
184  for (i = 0; i < AVPALETTE_COUNT && buf + 3 <= buf_end; i++) {
185  s->palette[i] = AV_RB24(buf) << 2;
186  buf += 3;
187  }
188  s->palette_has_changed = 1;
189  }
190 
191  if (video_size) {
192  s->frame->key_frame = 1;
195  s->palette_has_changed = 0;
196  memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
197 
198  if ((ret = av_frame_ref(data, s->frame)) < 0)
199  return ret;
200  *got_frame = 1;
201  }
202 
203  return buf_size;
204 }
205 
207 {
208  JvContext *s = avctx->priv_data;
209 
210  av_frame_free(&s->frame);
211 
212  return 0;
213 }
214 
216  .name = "jv",
217  .long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV video"),
218  .type = AVMEDIA_TYPE_VIDEO,
219  .id = AV_CODEC_ID_JV,
220  .priv_data_size = sizeof(JvContext),
221  .init = decode_init,
222  .close = decode_close,
223  .decode = decode_frame,
224  .capabilities = CODEC_CAP_DR1,
225 };
#define AVPALETTE_SIZE
Definition: avcodec.h:2959
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2440
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:107
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
int size
Definition: avcodec.h:974
#define AV_RB24
Definition: intreadwrite.h:64
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1247
#define AVPALETTE_COUNT
Definition: avcodec.h:2960
int palette_has_changed
Definition: jvdec.c:39
static av_cold int decode_init(AVCodecContext *avctx)
Definition: jvdec.c:42
AVCodec.
Definition: avcodec.h:2755
AVFrame * frame
Definition: jvdec.c:37
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:269
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:43
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
uint32_t palette[AVPALETTE_COUNT]
Definition: jvdec.c:38
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:174
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:711
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:973
bitstream reader API header.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:55
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
AVCodec ff_jv_decoder
Definition: jvdec.c:215
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
const char * name
Name of the codec implementation.
Definition: avcodec.h:2762
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: jvdec.c:139
static void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize, DSPContext *dsp)
Decode 8x8 block.
Definition: jvdec.c:115
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
Definition: utils.c:755
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:168
#define FFMIN(a, b)
Definition: common.h:57
int width
picture width / height.
Definition: avcodec.h:1217
#define AV_RL32
Definition: intreadwrite.h:146
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:125
main external API structure.
Definition: avcodec.h:1054
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:489
op_fill_func fill_block_tab[2]
Definition: dsputil.h:290
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:271
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:302
DSPContext dsp
Definition: jvdec.c:36
static av_cold int decode_close(AVCodecContext *avctx)
Definition: jvdec.c:206
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:113
static void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
Decode 2x2 block.
Definition: jvdec.c:58
common internal api header.
static void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
Decode 4x4 block.
Definition: jvdec.c:85
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:498
DSP utils.
void * priv_data
Definition: avcodec.h:1090
static int64_t video_size
Definition: avconv.c:87
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:163
This structure stores compressed data.
Definition: avcodec.h:950
DSPContext.
Definition: dsputil.h:124