libavcodec/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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Bitmap Brothers JV video decoder
25  * @author Peter Ross <pross@xvid.org>
26  */
27 
28 #include "avcodec.h"
29 #include "dsputil.h"
30 #include "get_bits.h"
31 #include "internal.h"
32 #include "libavutil/intreadwrite.h"
33 
34 typedef struct JvContext {
39 } JvContext;
40 
42 {
43  JvContext *s = avctx->priv_data;
44  avctx->pix_fmt = AV_PIX_FMT_PAL8;
45  ff_dsputil_init(&s->dsp, avctx);
47  return 0;
48 }
49 
50 /**
51  * Decode 2x2 block
52  */
53 static inline void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
54 {
55  int i, j, v[2];
56 
57  switch (get_bits(gb, 2)) {
58  case 1:
59  v[0] = get_bits(gb, 8);
60  for (j = 0; j < 2; j++)
61  memset(dst + j*linesize, v[0], 2);
62  break;
63  case 2:
64  v[0] = get_bits(gb, 8);
65  v[1] = get_bits(gb, 8);
66  for (j = 0; j < 2; j++)
67  for (i = 0; i < 2; i++)
68  dst[j*linesize + i] = v[get_bits1(gb)];
69  break;
70  case 3:
71  for (j = 0; j < 2; j++)
72  for (i = 0; i < 2; i++)
73  dst[j*linesize + i] = get_bits(gb, 8);
74  }
75 }
76 
77 /**
78  * Decode 4x4 block
79  */
80 static inline void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
81 {
82  int i, j, v[2];
83 
84  switch (get_bits(gb, 2)) {
85  case 1:
86  v[0] = get_bits(gb, 8);
87  for (j = 0; j < 4; j++)
88  memset(dst + j*linesize, v[0], 4);
89  break;
90  case 2:
91  v[0] = get_bits(gb, 8);
92  v[1] = get_bits(gb, 8);
93  for (j = 2; j >= 0; j -= 2) {
94  for (i = 0; i < 4; i++)
95  dst[j*linesize + i] = v[get_bits1(gb)];
96  for (i = 0; i < 4; i++)
97  dst[(j+1)*linesize + i] = v[get_bits1(gb)];
98  }
99  break;
100  case 3:
101  for (j = 0; j < 4; j += 2)
102  for (i = 0; i < 4; i += 2)
103  decode2x2(gb, dst + j*linesize + i, linesize);
104  }
105 }
106 
107 /**
108  * Decode 8x8 block
109  */
110 static inline void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize, DSPContext *dsp)
111 {
112  int i, j, v[2];
113 
114  switch (get_bits(gb, 2)) {
115  case 1:
116  v[0] = get_bits(gb, 8);
117  dsp->fill_block_tab[1](dst, v[0], linesize, 8);
118  break;
119  case 2:
120  v[0] = get_bits(gb, 8);
121  v[1] = get_bits(gb, 8);
122  for (j = 7; j >= 0; j--)
123  for (i = 0; i < 8; i++)
124  dst[j*linesize + i] = v[get_bits1(gb)];
125  break;
126  case 3:
127  for (j = 0; j < 8; j += 4)
128  for (i = 0; i < 8; i += 4)
129  decode4x4(gb, dst + j*linesize + i, linesize);
130  }
131 }
132 
133 static int decode_frame(AVCodecContext *avctx,
134  void *data, int *got_frame,
135  AVPacket *avpkt)
136 {
137  JvContext *s = avctx->priv_data;
138  const uint8_t *buf = avpkt->data;
139  const uint8_t *buf_end = buf + avpkt->size;
140  int video_size, video_type, i, j, ret;
141 
142  if (avpkt->size < 6)
143  return AVERROR_INVALIDDATA;
144 
145  video_size = AV_RL32(buf);
146  video_type = buf[4];
147  buf += 5;
148 
149  if (video_size) {
150  if (video_size < 0 || video_size > avpkt->size - 5) {
151  av_log(avctx, AV_LOG_ERROR, "video size %d invalid\n", video_size);
152  return AVERROR_INVALIDDATA;
153  }
154  if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0)
155  return ret;
156 
157  if (video_type == 0 || video_type == 1) {
158  GetBitContext gb;
159  init_get_bits(&gb, buf, 8 * video_size);
160 
161  for (j = 0; j < avctx->height; j += 8)
162  for (i = 0; i < avctx->width; i += 8)
163  decode8x8(&gb, s->frame.data[0] + j*s->frame.linesize[0] + i,
164  s->frame.linesize[0], &s->dsp);
165 
166  buf += video_size;
167  } else if (video_type == 2) {
168  int v = *buf++;
169  for (j = 0; j < avctx->height; j++)
170  memset(s->frame.data[0] + j*s->frame.linesize[0], v, avctx->width);
171  } else {
172  av_log(avctx, AV_LOG_WARNING, "unsupported frame type %i\n", video_type);
173  return AVERROR_INVALIDDATA;
174  }
175  }
176 
177  if (buf_end - buf >= AVPALETTE_COUNT * 3) {
178  for (i = 0; i < AVPALETTE_COUNT; i++) {
179  uint32_t pal = AV_RB24(buf);
180  s->palette[i] = 0xFFU << 24 | pal << 2 | ((pal >> 4) & 0x30303);
181  buf += 3;
182  }
183  s->palette_has_changed = 1;
184  }
185 
186  if (video_size) {
187  s->frame.key_frame = 1;
190  s->palette_has_changed = 0;
191  memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
192 
193  if ((ret = av_frame_ref(data, &s->frame)) < 0)
194  return ret;
195  *got_frame = 1;
196  }
197 
198  return avpkt->size;
199 }
200 
202 {
203  JvContext *s = avctx->priv_data;
204 
205  av_frame_unref(&s->frame);
206 
207  return 0;
208 }
209 
211  .name = "jv",
212  .long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV video"),
213  .type = AVMEDIA_TYPE_VIDEO,
214  .id = AV_CODEC_ID_JV,
215  .priv_data_size = sizeof(JvContext),
216  .init = decode_init,
217  .close = decode_close,
218  .decode = decode_frame,
219  .capabilities = CODEC_CAP_DR1,
220 };
float v
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2675
const char * s
Definition: avisynth_c.h:668
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
#define AV_RB24
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int palette_has_changed
struct JvContext JvContext
static av_cold int decode_init(AVCodecContext *avctx)
uint8_t
#define av_cold
Definition: attributes.h:78
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:79
uint32_t palette[AVPALETTE_COUNT]
#define AVPALETTE_SIZE
Definition: pixfmt.h:33
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
uint8_t * data
bitstream reader API header.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
AVCodec ff_jv_decoder
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
external API header
static void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize, DSPContext *dsp)
Decode 8x8 block.
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...
static int64_t video_size
Definition: ffmpeg.c:125
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:144
ret
Definition: avfilter.c:821
int width
picture width / height.
#define AV_RL32
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
main external API structure.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
op_fill_func fill_block_tab[2]
Definition: dsputil.h:313
void * buf
Definition: avisynth_c.h:594
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:273
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
synthesis window for stochastic i
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:280
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:330
DSPContext dsp
#define AVPALETTE_COUNT
Definition: pixfmt.h:34
static av_cold int decode_close(AVCodecContext *avctx)
int av_frame_ref(AVFrame *dst, AVFrame *src)
Setup a new reference to the data described by an given frame.
Definition: frame.c:228
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
static void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
Decode 2x2 block.
common internal api header.
static void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
Decode 4x4 block.
DSP utils.
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:139
AVFrame frame
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
This structure stores compressed data.
DSPContext.
Definition: dsputil.h:127