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 "codec_internal.h"
33 #include "decode.h"
34 #include "get_bits.h"
35 
36 typedef struct JvContext {
40 #if FF_API_PALETTE_HAS_CHANGED
41  int palette_has_changed;
42 #endif
43 } JvContext;
44 
46 {
47  JvContext *s = avctx->priv_data;
48 
49  if (!avctx->width || !avctx->height ||
50  (avctx->width & 7) || (avctx->height & 7)) {
51  av_log(avctx, AV_LOG_ERROR, "Invalid video dimensions: %dx%d\n",
52  avctx->width, avctx->height);
53  return AVERROR(EINVAL);
54  }
55 
56  s->frame = av_frame_alloc();
57  if (!s->frame)
58  return AVERROR(ENOMEM);
59 
60  avctx->pix_fmt = AV_PIX_FMT_PAL8;
61  ff_blockdsp_init(&s->bdsp);
62  return 0;
63 }
64 
65 /**
66  * Decode 2x2 block
67  */
68 static inline void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
69 {
70  int i, j, v[2];
71 
72  switch (get_bits(gb, 2)) {
73  case 1:
74  v[0] = get_bits(gb, 8);
75  for (j = 0; j < 2; j++)
76  memset(dst + j * linesize, v[0], 2);
77  break;
78  case 2:
79  v[0] = get_bits(gb, 8);
80  v[1] = get_bits(gb, 8);
81  for (j = 0; j < 2; j++)
82  for (i = 0; i < 2; i++)
83  dst[j * linesize + i] = v[get_bits1(gb)];
84  break;
85  case 3:
86  for (j = 0; j < 2; j++)
87  for (i = 0; i < 2; i++)
88  dst[j * linesize + i] = get_bits(gb, 8);
89  }
90 }
91 
92 /**
93  * Decode 4x4 block
94  */
95 static inline void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
96 {
97  int i, j, v[2];
98 
99  switch (get_bits(gb, 2)) {
100  case 1:
101  v[0] = get_bits(gb, 8);
102  for (j = 0; j < 4; j++)
103  memset(dst + j * linesize, v[0], 4);
104  break;
105  case 2:
106  v[0] = get_bits(gb, 8);
107  v[1] = get_bits(gb, 8);
108  for (j = 2; j >= 0; j -= 2) {
109  for (i = 0; i < 4; i++)
110  dst[j * linesize + i] = v[get_bits1(gb)];
111  for (i = 0; i < 4; i++)
112  dst[(j + 1) * linesize + i] = v[get_bits1(gb)];
113  }
114  break;
115  case 3:
116  for (j = 0; j < 4; j += 2)
117  for (i = 0; i < 4; i += 2)
118  decode2x2(gb, dst + j * linesize + i, linesize);
119  }
120 }
121 
122 /**
123  * Decode 8x8 block
124  */
125 static inline void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize,
126  BlockDSPContext *bdsp)
127 {
128  int i, j, v[2];
129 
130  switch (get_bits(gb, 2)) {
131  case 1:
132  v[0] = get_bits(gb, 8);
133  bdsp->fill_block_tab[1](dst, v[0], linesize, 8);
134  break;
135  case 2:
136  v[0] = get_bits(gb, 8);
137  v[1] = get_bits(gb, 8);
138  for (j = 7; j >= 0; j--)
139  for (i = 0; i < 8; i++)
140  dst[j * linesize + i] = v[get_bits1(gb)];
141  break;
142  case 3:
143  for (j = 0; j < 8; j += 4)
144  for (i = 0; i < 8; i += 4)
145  decode4x4(gb, dst + j * linesize + i, linesize);
146  }
147 }
148 
149 static int decode_frame(AVCodecContext *avctx, AVFrame *rframe,
150  int *got_frame, AVPacket *avpkt)
151 {
152  JvContext *s = avctx->priv_data;
153  const uint8_t *buf = avpkt->data;
154  const uint8_t *buf_end = buf + avpkt->size;
155  int video_size, video_type, i, j, ret;
156 
157  if (avpkt->size < 6)
158  return AVERROR_INVALIDDATA;
159 
160  video_size = AV_RL32(buf);
161  video_type = buf[4];
162  buf += 5;
163 
164  if (video_size) {
165  if (video_size < 0 || video_size > avpkt->size - 5) {
166  av_log(avctx, AV_LOG_ERROR, "video size %d invalid\n", video_size);
167  return AVERROR_INVALIDDATA;
168  }
169 
170  if (video_type == 0 || video_type == 1) {
171  GetBitContext gb;
172  init_get_bits(&gb, buf, 8 * video_size);
173 
174  if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
175  return ret;
176 
177  if (avctx->height/8 * (avctx->width/8) > 4 * video_size) {
178  av_log(avctx, AV_LOG_ERROR, "Insufficient input data for dimensions\n");
179  return AVERROR_INVALIDDATA;
180  }
181 
182  for (j = 0; j < avctx->height; j += 8)
183  for (i = 0; i < avctx->width; i += 8)
184  decode8x8(&gb,
185  s->frame->data[0] + j * s->frame->linesize[0] + i,
186  s->frame->linesize[0], &s->bdsp);
187 
188  buf += video_size;
189  } else if (video_type == 2) {
190  int v = *buf++;
191 
192  av_frame_unref(s->frame);
193  if ((ret = ff_get_buffer(avctx, s->frame, AV_GET_BUFFER_FLAG_REF)) < 0)
194  return ret;
195 
196  for (j = 0; j < avctx->height; j++)
197  memset(s->frame->data[0] + j * s->frame->linesize[0],
198  v, avctx->width);
199  } else {
200  av_log(avctx, AV_LOG_WARNING,
201  "unsupported frame type %i\n", video_type);
202  return AVERROR_INVALIDDATA;
203  }
204  }
205 
206  if (buf_end - buf >= AVPALETTE_COUNT * 3) {
207  for (i = 0; i < AVPALETTE_COUNT; i++) {
208  uint32_t pal = AV_RB24(buf);
209  s->palette[i] = 0xFFU << 24 | pal << 2 | ((pal >> 4) & 0x30303);
210  buf += 3;
211  }
212 #if FF_API_PALETTE_HAS_CHANGED
213  s->palette_has_changed = 1;
214 #endif
215  }
216 
217  if (video_size) {
218 #if FF_API_PALETTE_HAS_CHANGED
220  s->frame->palette_has_changed = s->palette_has_changed;
221  s->palette_has_changed = 0;
223 #endif
224  memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
225 
226  if ((ret = av_frame_ref(rframe, s->frame)) < 0)
227  return ret;
228  *got_frame = 1;
229  }
230 
231  return avpkt->size;
232 }
233 
235 {
236  JvContext *s = avctx->priv_data;
237 
238  av_frame_free(&s->frame);
239 
240  return 0;
241 }
242 
244  .p.name = "jv",
245  CODEC_LONG_NAME("Bitmap Brothers JV video"),
246  .p.type = AVMEDIA_TYPE_VIDEO,
247  .p.id = AV_CODEC_ID_JV,
248  .priv_data_size = sizeof(JvContext),
249  .init = decode_init,
250  .close = decode_close,
252  .p.capabilities = AV_CODEC_CAP_DR1,
253 };
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
BlockDSPContext::fill_block_tab
op_fill_func fill_block_tab[2]
Definition: blockdsp.h:36
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
blockdsp.h
AVERROR
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
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
decode2x2
static void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
Decode 2x2 block.
Definition: jvdec.c:68
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:403
AVPacket::data
uint8_t * data
Definition: packet.h:539
FFCodec
Definition: codec_internal.h:127
BlockDSPContext
Definition: blockdsp.h:32
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:514
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
GetBitContext
Definition: get_bits.h:108
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:151
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:311
ff_blockdsp_init
av_cold void ff_blockdsp_init(BlockDSPContext *c)
Definition: blockdsp.c:58
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_GET_BUFFER_FLAG_REF
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:431
ff_jv_decoder
const FFCodec ff_jv_decoder
Definition: jvdec.c:243
decode.h
get_bits.h
JvContext::bdsp
BlockDSPContext bdsp
Definition: jvdec.c:37
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
AVPALETTE_SIZE
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
AVPALETTE_COUNT
#define AVPALETTE_COUNT
Definition: pixfmt.h:33
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition: jvdec.c:149
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1697
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:540
av_frame_ref
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:401
codec_internal.h
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
decode_close
static av_cold int decode_close(AVCodecContext *avctx)
Definition: jvdec.c:234
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
JvContext::palette
uint32_t palette[AVPALETTE_COUNT]
Definition: jvdec.c:39
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:623
JvContext::frame
AVFrame * frame
Definition: jvdec.c:38
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
JvContext
Definition: jvdec.c:36
AVCodecContext::height
int height
Definition: avcodec.h:632
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:671
avcodec.h
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:84
ff_reget_buffer
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition: decode.c:1815
ret
ret
Definition: filter_design.txt:187
AV_CODEC_ID_JV
@ AV_CODEC_ID_JV
Definition: codec_id.h:201
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
U
#define U(x)
Definition: vpx_arith.h:37
AVCodecContext
main external API structure.
Definition: avcodec.h:451
decode8x8
static void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize, BlockDSPContext *bdsp)
Decode 8x8 block.
Definition: jvdec.c:125
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: jvdec.c:45
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:632
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
decode4x4
static void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
Decode 4x4 block.
Definition: jvdec.c:95
AV_RB24
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:97