FFmpeg: libavcodec/vble.c Source File

FFmpeg
vble.c
Go to the documentation of this file.
1 /*
2  * VBLE Decoder
3  * Copyright (c) 2011 Derek Buitenhuis
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  * VBLE Decoder
25  */
26 
27 #include "libavutil/imgutils.h"
28 
29  #define BITSTREAM_READER_LE
30 #include "avcodec.h"
31 #include "get_bits.h"
32 #include "internal.h"
33 #include "lossless_videodsp.h"
34 #include "mathops.h"
35 #include "thread.h"
36 
37  typedef struct VBLEContext {
38   AVCodecContext *avctx;
39   LLVidDSPContext llviddsp;
40 
41   int size;
42   uint8_t *val; ///< This array first holds the lengths of vlc symbols and then their value.
43 } VBLEContext;
44 
45  static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
46 {
47  int i;
48  int allbits = 0;
49  static const uint8_t LUT[256] = {
50  8,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
51  5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
52  6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
53  5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
54  7,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
55  5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
56  6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
57  5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
58  };
59 
60  /* Read all the lengths in first */
61  for (i = 0; i < ctx->size; i++) {
62  /* At most we need to read 9 bits total to get indices up to 8 */
63  int val = show_bits(gb, 8);
64 
65  // read reverse unary
66  if (val) {
67  val = LUT[val];
68  skip_bits(gb, val + 1);
69  ctx->val[i] = val;
70  } else {
71  skip_bits(gb, 8);
72  if (!get_bits1(gb))
73  return -1;
74  ctx->val[i] = 8;
75  }
76  allbits += ctx->val[i];
77  }
78 
79  /* Check we have enough bits left */
80  if (get_bits_left(gb) < allbits)
81  return -1;
82  return 0;
83 }
84 
85  static void vble_restore_plane(VBLEContext *ctx, AVFrame *pic,
86  GetBitContext *gb, int plane,
87  int offset, int width, int height)
88 {
89  uint8_t *dst = pic->data[plane];
90  uint8_t *val = ctx->val + offset;
91  int stride = pic->linesize[plane];
92  int i, j, left, left_top;
93 
94  for (i = 0; i < height; i++) {
95  for (j = 0; j < width; j++) {
96  /* get_bits can't take a length of 0 */
97  if (val[j]) {
98  int v = (1 << val[j]) + get_bits(gb, val[j]) - 1;
99  val[j] = (v >> 1) ^ -(v & 1);
100  }
101  }
102  if (i) {
103  left = 0;
104  left_top = dst[-stride];
105  ctx->llviddsp.add_median_pred(dst, dst - stride, val,
106  width, &left, &left_top);
107  } else {
108  dst[0] = val[0];
109  for (j = 1; j < width; j++)
110  dst[j] = val[j] + dst[j - 1];
111  }
112  dst += stride;
113  val += width;
114  }
115 }
116 
117  static int vble_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
118  AVPacket *avpkt)
119 {
120  VBLEContext *ctx = avctx->priv_data;
121  AVFrame *pic = data;
122  GetBitContext gb;
123  const uint8_t *src = avpkt->data;
124  int version;
125  int offset = 0;
126  int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
127  int ret;
128  ThreadFrame frame = { .f = data };
129 
130  if (avpkt->size < 4 || avpkt->size - 4 > INT_MAX/8) {
131  av_log(avctx, AV_LOG_ERROR, "Invalid packet size\n");
132  return AVERROR_INVALIDDATA;
133  }
134 
135  /* Allocate buffer */
136  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
137  return ret;
138 
139  /* Set flags */
140  pic->key_frame = 1;
141  pic->pict_type = AV_PICTURE_TYPE_I;
142 
143  /* Version should always be 1 */
144  version = AV_RL32(src);
145 
146  if (version != 1)
147  av_log(avctx, AV_LOG_WARNING, "Unsupported VBLE Version: %d\n", version);
148 
149  init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8);
150 
151  /* Unpack */
152  if (vble_unpack(ctx, &gb) < 0) {
153  av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
154  return AVERROR_INVALIDDATA;
155  }
156 
157  /* Restore planes. Should be almost identical to Huffyuv's. */
158  vble_restore_plane(ctx, pic, &gb, 0, offset, avctx->width, avctx->height);
159 
160  /* Chroma */
161  if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) {
162  offset += avctx->width * avctx->height;
163  vble_restore_plane(ctx, pic, &gb, 1, offset, width_uv, height_uv);
164 
165  offset += width_uv * height_uv;
166  vble_restore_plane(ctx, pic, &gb, 2, offset, width_uv, height_uv);
167  }
168 
169  *got_frame = 1;
170 
171  return avpkt->size;
172 }
173 
174  static av_cold int vble_decode_close(AVCodecContext *avctx)
175 {
176  VBLEContext *ctx = avctx->priv_data;
177  av_freep(&ctx->val);
178 
179  return 0;
180 }
181 
182  static av_cold int vble_decode_init(AVCodecContext *avctx)
183 {
184  VBLEContext *ctx = avctx->priv_data;
185 
186  /* Stash for later use */
187  ctx->avctx = avctx;
188  ff_llviddsp_init(&ctx->llviddsp);
189 
190  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
191  avctx->bits_per_raw_sample = 8;
192 
193  ctx->size = av_image_get_buffer_size(avctx->pix_fmt,
194  avctx->width, avctx->height, 1);
195 
196  ctx->val = av_malloc_array(ctx->size, sizeof(*ctx->val));
197 
198  if (!ctx->val) {
199  av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
200  vble_decode_close(avctx);
201  return AVERROR(ENOMEM);
202  }
203 
204  return 0;
205 }
206 
207  AVCodec ff_vble_decoder = {
208  .name = "vble",
209  .long_name = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),
210  .type = AVMEDIA_TYPE_VIDEO,
211  .id = AV_CODEC_ID_VBLE,
212  .priv_data_size = sizeof(VBLEContext),
213  .init = vble_decode_init,
214  .close = vble_decode_close,
215  .decode = vble_decode_frame,
216  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
217  .init_thread_copy = ONLY_IF_THREADS_ENABLED(vble_decode_init),
218  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
219 };
plane
int plane
Definition: avisynth_c.h:422
val
const char const char void * val
Definition: avisynth_c.h:771
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
data
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
imgutils.h
misc image utilities
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:269
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
init_thread_copy
static int init_thread_copy(AVCodecContext *avctx)
Definition: tta.c:392
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
ff_vble_decoder
AVCodec ff_vble_decoder
Definition: vble.c:207
vble_decode_frame
static int vble_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: vble.c:117
AVPacket::size
int size
Definition: avcodec.h:1431
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1727
version
int version
Definition: avisynth_c.h:766
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2741
src
#define src
Definition: vp8dsp.c:254
AVCodec
AVCodec.
Definition: avcodec.h:3408
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
uint8_t
uint8_t
Definition: audio_convert.c:194
av_cold
#define av_cold
Definition: attributes.h:82
thread.h
Multithreading support functions.
vble_unpack
static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
Definition: vble.c:45
frame
static AVFrame * frame
Definition: demuxing_decoding.c:53
height
#define height
AVPacket::data
uint8_t * data
Definition: avcodec.h:1430
get_bits.h
bitstream reader API header.
AV_CODEC_FLAG_GRAY
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:861
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:596
av_image_get_buffer_size
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters...
Definition: imgutils.c:431
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVERROR
#define AVERROR(e)
Definition: error.h:43
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1598
width
uint16_t width
Definition: gdv.c:47
AVCodec::name
const char * name
Name of the codec implementation.
Definition: avcodec.h:3415
offset
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1015
ONLY_IF_THREADS_ENABLED
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:225
AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:301
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:1690
VBLEContext
Definition: vble.c:37
ctx
AVFormatContext * ctx
Definition: movenc.c:48
show_bits
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:304
AVCodecContext::height
int height
Definition: avcodec.h:1690
VBLEContext::size
int size
Definition: vble.c:41
avcodec.h
Libavcodec external API header.
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:249
ff_thread_get_buffer
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
Definition: pthread_frame.c:965
AVCodecContext
main external API structure.
Definition: avcodec.h:1518
vble_decode_close
static av_cold int vble_decode_close(AVCodecContext *avctx)
Definition: vble.c:174
ff_llviddsp_init
void ff_llviddsp_init(LLVidDSPContext *c)
Definition: lossless_videodsp.c:112
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:321
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:314
VBLEContext::val
uint8_t * val
This array first holds the lengths of vlc symbols and then their value.
Definition: vble.c:42
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:433
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
stride
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
internal.h
common internal api header.
VBLEContext::llviddsp
LLVidDSPContext llviddsp
Definition: vble.c:39
VBLEContext::avctx
AVCodecContext * avctx
Definition: vble.c:38
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:1545
vble_restore_plane
static void vble_restore_plane(VBLEContext *ctx, AVFrame *pic, GetBitContext *gb, int plane, int offset, int width, int height)
Definition: vble.c:85
AVFrame::key_frame
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:296
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
vble_decode_init
static av_cold int vble_decode_init(AVCodecContext *avctx)
Definition: vble.c:182
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
stride
#define stride
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1407
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959
LLVidDSPContext::add_median_pred
void(* add_median_pred)(uint8_t *dst, const uint8_t *top, const uint8_t *diff, ptrdiff_t w, int *left, int *left_top)
Definition: lossless_videodsp.h:34

Generated on Sun May 13 2018 02:03:56 for FFmpeg by   doxygen 1.8.6

AltStyle によって変換されたページ (->オリジナル) /