FFmpeg
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 "libavutil/intreadwrite.h"
29 
30 #include "avcodec.h"
31 #include "blockdsp.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  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  s->frame = av_frame_alloc();
54  if (!s->frame)
55  return AVERROR(ENOMEM);
56 
57  avctx->pix_fmt = AV_PIX_FMT_PAL8;
58  ff_blockdsp_init(&s->bdsp, avctx);
59  return 0;
60 }
61 
62 /**
63  * Decode 2x2 block
64  */
65 static inline void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
66 {
67  int i, j, v[2];
68 
69  switch (get_bits(gb, 2)) {
70  case 1:
71  v[0] = get_bits(gb, 8);
72  for (j = 0; j < 2; j++)
73  memset(dst + j * linesize, v[0], 2);
74  break;
75  case 2:
76  v[0] = get_bits(gb, 8);
77  v[1] = get_bits(gb, 8);
78  for (j = 0; j < 2; j++)
79  for (i = 0; i < 2; i++)
80  dst[j * linesize + i] = v[get_bits1(gb)];
81  break;
82  case 3:
83  for (j = 0; j < 2; j++)
84  for (i = 0; i < 2; i++)
85  dst[j * linesize + i] = get_bits(gb, 8);
86  }
87 }
88 
89 /**
90  * Decode 4x4 block
91  */
92 static inline void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
93 {
94  int i, j, v[2];
95 
96  switch (get_bits(gb, 2)) {
97  case 1:
98  v[0] = get_bits(gb, 8);
99  for (j = 0; j < 4; j++)
100  memset(dst + j * linesize, v[0], 4);
101  break;
102  case 2:
103  v[0] = get_bits(gb, 8);
104  v[1] = get_bits(gb, 8);
105  for (j = 2; j >= 0; j -= 2) {
106  for (i = 0; i < 4; i++)
107  dst[j * linesize + i] = v[get_bits1(gb)];
108  for (i = 0; i < 4; i++)
109  dst[(j + 1) * linesize + i] = v[get_bits1(gb)];
110  }
111  break;
112  case 3:
113  for (j = 0; j < 4; j += 2)
114  for (i = 0; i < 4; i += 2)
115  decode2x2(gb, dst + j * linesize + i, linesize);
116  }
117 }
118 
119 /**
120  * Decode 8x8 block
121  */
122 static inline void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize,
124 {
125  int i, j, v[2];
126 
127  switch (get_bits(gb, 2)) {
128  case 1:
129  v[0] = get_bits(gb, 8);
130  bdsp->fill_block_tab[1](dst, v[0], linesize, 8);
131  break;
132  case 2:
133  v[0] = get_bits(gb, 8);
134  v[1] = get_bits(gb, 8);
135  for (j = 7; j >= 0; j--)
136  for (i = 0; i < 8; i++)
137  dst[j * linesize + i] = v[get_bits1(gb)];
138  break;
139  case 3:
140  for (j = 0; j < 8; j += 4)
141  for (i = 0; i < 8; i += 4)
142  decode4x4(gb, dst + j * linesize + i, linesize);
143  }
144 }
145 
146 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
147  AVPacket *avpkt)
148 {
149  JvContext *s = avctx->priv_data;
150  const uint8_t *buf = avpkt->data;
151  const uint8_t *buf_end = buf + avpkt->size;
152  int video_size, video_type, i, j, ret;
153 
154  if (avpkt->size < 6)
155  return AVERROR_INVALIDDATA;
156 
157  video_size = AV_RL32(buf);
158  video_type = buf[4];
159  buf += 5;
160 
161  if (video_size) {
162  if (video_size < 0 || video_size > avpkt->size - 5) {
163  av_log(avctx, AV_LOG_ERROR, "video size %d invalid\n", video_size);
164  return AVERROR_INVALIDDATA;
165  }
166 
167  if (video_type == 0 || video_type == 1) {
168  GetBitContext gb;
169  init_get_bits(&gb, buf, 8 * video_size);
170 
171  if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
172  return ret;
173 
174  if (avctx->height/8 * (avctx->width/8) > 4 * video_size) {
175  av_log(avctx, AV_LOG_ERROR, "Insufficient input data for dimensions\n");
176  return AVERROR_INVALIDDATA;
177  }
178 
179  for (j = 0; j < avctx->height; j += 8)
180  for (i = 0; i < avctx->width; i += 8)
181  decode8x8(&gb,
182  s->frame->data[0] + j * s->frame->linesize[0] + i,
183  s->frame->linesize[0], &s->bdsp);
184 
185  buf += video_size;
186  } else if (video_type == 2) {
187  int v = *buf++;
188 
189  av_frame_unref(s->frame);
190  if ((ret = ff_get_buffer(avctx, s->frame, AV_GET_BUFFER_FLAG_REF)) < 0)
191  return ret;
192 
193  for (j = 0; j < avctx->height; j++)
194  memset(s->frame->data[0] + j * s->frame->linesize[0],
195  v, avctx->width);
196  } else {
197  av_log(avctx, AV_LOG_WARNING,
198  "unsupported frame type %i\n", video_type);
199  return AVERROR_INVALIDDATA;
200  }
201  }
202 
203  if (buf_end - buf >= AVPALETTE_COUNT * 3) {
204  for (i = 0; i < AVPALETTE_COUNT; i++) {
205  uint32_t pal = AV_RB24(buf);
206  s->palette[i] = 0xFFU << 24 | pal << 2 | ((pal >> 4) & 0x30303);
207  buf += 3;
208  }
209  s->palette_has_changed = 1;
210  }
211 
212  if (video_size) {
213  s->frame->key_frame = 1;
216  s->palette_has_changed = 0;
217  memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
218 
219  if ((ret = av_frame_ref(data, s->frame)) < 0)
220  return ret;
221  *got_frame = 1;
222  }
223 
224  return avpkt->size;
225 }
226 
228 {
229  JvContext *s = avctx->priv_data;
230 
231  av_frame_free(&s->frame);
232 
233  return 0;
234 }
235 
237  .name = "jv",
238  .long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV video"),
239  .type = AVMEDIA_TYPE_VIDEO,
240  .id = AV_CODEC_ID_JV,
241  .priv_data_size = sizeof(JvContext),
242  .init = decode_init,
243  .close = decode_close,
244  .decode = decode_frame,
245  .capabilities = AV_CODEC_CAP_DR1,
246 };
#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:295
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1478
av_cold void ff_blockdsp_init(BlockDSPContext *c, AVCodecContext *avctx)
Definition: blockdsp.c:60
static void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize, BlockDSPContext *bdsp)
Decode 8x8 block.
Definition: jvdec.c:122
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1775
int palette_has_changed
Definition: jvdec.c:39
static av_cold int decode_init(AVCodecContext *avctx)
Definition: jvdec.c:42
AVCodec.
Definition: avcodec.h:3481
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
AVFrame * frame
Definition: jvdec.c:37
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: decode.c:2011
uint8_t
#define av_cold
Definition: attributes.h:82
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
uint32_t palette[AVPALETTE_COUNT]
Definition: jvdec.c:38
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
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:443
uint8_t * data
Definition: avcodec.h:1477
bitstream reader API header.
#define av_log(a,...)
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
AVCodec ff_jv_decoder
Definition: jvdec.c:236
const char * name
Name of the codec implementation.
Definition: avcodec.h:3488
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: jvdec.c:146
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:378
int width
picture width / height.
Definition: avcodec.h:1738
#define s(width, name)
Definition: cbs_vp9.c:257
Libavcodec external API header.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:87
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:326
main external API structure.
Definition: avcodec.h:1565
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1964
void * buf
Definition: avisynth_c.h:766
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:452
#define AVPALETTE_COUNT
Definition: pixfmt.h:33
static av_cold int decode_close(AVCodecContext *avctx)
Definition: jvdec.c:227
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
static void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
Decode 2x2 block.
Definition: jvdec.c:65
common internal api header.
static void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
Decode 4x4 block.
Definition: jvdec.c:92
void * priv_data
Definition: avcodec.h:1592
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:373
BlockDSPContext bdsp
Definition: jvdec.c:36
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1454
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1176
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:981
op_fill_func fill_block_tab[2]
Definition: blockdsp.h:39