Libav
hapdec.c
Go to the documentation of this file.
1 /*
2  * Vidvox Hap decoder
3  * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
4  * Copyright (C) 2015 Tom Butterworth <bangnoise@gmail.com>
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
32 #include <stdint.h>
33 
34 #include "libavutil/imgutils.h"
35 
36 #include "avcodec.h"
37 #include "bytestream.h"
38 #include "hap.h"
39 #include "internal.h"
40 #include "memory.h"
41 #include "snappy.h"
42 #include "texturedsp.h"
43 #include "thread.h"
44 
45 /* The first three bytes are the size of the section past the header, or zero
46  * if the length is stored in the next long word. The fourth byte in the first
47  * long word indicates the type of the current section. */
48 static int parse_section_header(GetByteContext *gbc, int *section_size,
49  enum HapSectionType *section_type)
50 {
51  if (bytestream2_get_bytes_left(gbc) < 4)
52  return AVERROR_INVALIDDATA;
53 
54  *section_size = bytestream2_get_le24(gbc);
55  *section_type = bytestream2_get_byte(gbc);
56 
57  if (*section_size == 0) {
58  if (bytestream2_get_bytes_left(gbc) < 4)
59  return AVERROR_INVALIDDATA;
60 
61  *section_size = bytestream2_get_le32(gbc);
62  }
63 
64  if (*section_size > bytestream2_get_bytes_left(gbc))
65  return AVERROR_INVALIDDATA;
66  else
67  return 0;
68 }
69 
71 {
72  GetByteContext *gbc = &ctx->gbc;
73  int section_size;
74  enum HapSectionType section_type;
75  int is_first_table = 1, had_offsets = 0, had_compressors = 0, had_sizes = 0;
76  int i, ret;
77 
78  while (size > 0) {
79  int stream_remaining = bytestream2_get_bytes_left(gbc);
80  ret = parse_section_header(gbc, &section_size, &section_type);
81  if (ret != 0)
82  return ret;
83 
84  size -= stream_remaining - bytestream2_get_bytes_left(gbc);
85 
86  switch (section_type) {
88  ret = ff_hap_set_chunk_count(ctx, section_size, is_first_table);
89  if (ret != 0)
90  return ret;
91  for (i = 0; i < section_size; i++) {
92  ctx->chunks[i].compressor = bytestream2_get_byte(gbc) << 4;
93  }
94  had_compressors = 1;
95  is_first_table = 0;
96  break;
97  case HAP_ST_SIZE_TABLE:
98  ret = ff_hap_set_chunk_count(ctx, section_size / 4, is_first_table);
99  if (ret != 0)
100  return ret;
101  for (i = 0; i < section_size / 4; i++) {
102  ctx->chunks[i].compressed_size = bytestream2_get_le32(gbc);
103  }
104  had_sizes = 1;
105  is_first_table = 0;
106  break;
107  case HAP_ST_OFFSET_TABLE:
108  ret = ff_hap_set_chunk_count(ctx, section_size / 4, is_first_table);
109  if (ret != 0)
110  return ret;
111  for (i = 0; i < section_size / 4; i++) {
112  ctx->chunks[i].compressed_offset = bytestream2_get_le32(gbc);
113  }
114  had_offsets = 1;
115  is_first_table = 0;
116  break;
117  default:
118  break;
119  }
120  size -= section_size;
121  }
122 
123  if (!had_sizes || !had_compressors)
124  return AVERROR_INVALIDDATA;
125 
126  /* The offsets table is optional. If not present than calculate offsets by
127  * summing the sizes of preceding chunks. */
128  if (!had_offsets) {
129  size_t running_size = 0;
130  for (i = 0; i < ctx->chunk_count; i++) {
131  ctx->chunks[i].compressed_offset = running_size;
132  running_size += ctx->chunks[i].compressed_size;
133  }
134  }
135 
136  return 0;
137 }
138 
140 {
141  int i;
142  size_t running_offset = 0;
143  for (i = 0; i < ctx->chunk_count; i++) {
144  if (ctx->chunks[i].compressed_offset != running_offset
145  || ctx->chunks[i].compressor != HAP_COMP_NONE)
146  return 0;
147  running_offset += ctx->chunks[i].compressed_size;
148  }
149  return 1;
150 }
151 
153 {
154  HapContext *ctx = avctx->priv_data;
155  GetByteContext *gbc = &ctx->gbc;
156  int section_size;
157  enum HapSectionType section_type;
158  const char *compressorstr;
159  int i, ret;
160 
161  ret = parse_section_header(gbc, &section_size, &section_type);
162  if (ret != 0)
163  return ret;
164 
165  if ((avctx->codec_tag == MKTAG('H','a','p','1') && (section_type & 0x0F) != HAP_FMT_RGBDXT1) ||
166  (avctx->codec_tag == MKTAG('H','a','p','5') && (section_type & 0x0F) != HAP_FMT_RGBADXT5) ||
167  (avctx->codec_tag == MKTAG('H','a','p','Y') && (section_type & 0x0F) != HAP_FMT_YCOCGDXT5)) {
168  av_log(avctx, AV_LOG_ERROR,
169  "Invalid texture format %#04x.\n", section_type & 0x0F);
170  return AVERROR_INVALIDDATA;
171  }
172 
173  switch (section_type & 0xF0) {
174  case HAP_COMP_NONE:
175  case HAP_COMP_SNAPPY:
176  ret = ff_hap_set_chunk_count(ctx, 1, 1);
177  if (ret == 0) {
178  ctx->chunks[0].compressor = section_type & 0xF0;
179  ctx->chunks[0].compressed_offset = 0;
180  ctx->chunks[0].compressed_size = section_size;
181  }
182  if (ctx->chunks[0].compressor == HAP_COMP_NONE) {
183  compressorstr = "none";
184  } else {
185  compressorstr = "snappy";
186  }
187  break;
188  case HAP_COMP_COMPLEX:
189  ret = parse_section_header(gbc, &section_size, &section_type);
190  if (ret == 0 && section_type != HAP_ST_DECODE_INSTRUCTIONS)
191  ret = AVERROR_INVALIDDATA;
192  if (ret == 0)
193  ret = hap_parse_decode_instructions(ctx, section_size);
194  compressorstr = "complex";
195  break;
196  default:
197  ret = AVERROR_INVALIDDATA;
198  break;
199  }
200 
201  if (ret != 0)
202  return ret;
203 
204  /* Check the frame is valid and read the uncompressed chunk sizes */
205  ctx->tex_size = 0;
206  for (i = 0; i < ctx->chunk_count; i++) {
207  HapChunk *chunk = &ctx->chunks[i];
208 
209  /* Check the compressed buffer is valid */
211  return AVERROR_INVALIDDATA;
212 
213  /* Chunks are unpacked sequentially, ctx->tex_size is the uncompressed
214  * size thus far */
215  chunk->uncompressed_offset = ctx->tex_size;
216 
217  /* Fill out uncompressed size */
218  if (chunk->compressor == HAP_COMP_SNAPPY) {
219  GetByteContext gbc_tmp;
220  int64_t uncompressed_size;
221  bytestream2_init(&gbc_tmp, gbc->buffer + chunk->compressed_offset,
222  chunk->compressed_size);
223  uncompressed_size = ff_snappy_peek_uncompressed_length(&gbc_tmp);
224  if (uncompressed_size < 0) {
225  return uncompressed_size;
226  }
227  chunk->uncompressed_size = uncompressed_size;
228  } else if (chunk->compressor == HAP_COMP_NONE) {
229  chunk->uncompressed_size = chunk->compressed_size;
230  } else {
231  return AVERROR_INVALIDDATA;
232  }
233  ctx->tex_size += chunk->uncompressed_size;
234  }
235 
236  av_log(avctx, AV_LOG_DEBUG, "%s compressor\n", compressorstr);
237 
238  return ret;
239 }
240 
241 static int decompress_chunks_thread(AVCodecContext *avctx, void *arg,
242  int chunk_nb, int thread_nb)
243 {
244  HapContext *ctx = avctx->priv_data;
245 
246  HapChunk *chunk = &ctx->chunks[chunk_nb];
247  GetByteContext gbc;
248  uint8_t *dst = ctx->tex_buf + chunk->uncompressed_offset;
249 
250  bytestream2_init(&gbc, ctx->gbc.buffer + chunk->compressed_offset, chunk->compressed_size);
251 
252  if (chunk->compressor == HAP_COMP_SNAPPY) {
253  int ret;
254  int64_t uncompressed_size = ctx->tex_size;
255 
256  /* Uncompress the frame */
257  ret = ff_snappy_uncompress(&gbc, dst, &uncompressed_size);
258  if (ret < 0) {
259  av_log(avctx, AV_LOG_ERROR, "Snappy uncompress error\n");
260  return ret;
261  }
262  } else if (chunk->compressor == HAP_COMP_NONE) {
263  bytestream2_get_buffer(&gbc, dst, chunk->compressed_size);
264  }
265 
266  return 0;
267 }
268 
269 static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
270  int slice, int thread_nb)
271 {
272  HapContext *ctx = avctx->priv_data;
273  AVFrame *frame = arg;
274  const uint8_t *d = ctx->tex_data;
275  int w_block = avctx->coded_width / TEXTURE_BLOCK_W;
276  int h_block = avctx->coded_height / TEXTURE_BLOCK_H;
277  int x, y;
278  int start_slice, end_slice;
279  int base_blocks_per_slice = h_block / ctx->slice_count;
280  int remainder_blocks = h_block % ctx->slice_count;
281 
282  /* When the frame height (in blocks) doesn't divide evenly between the
283  * number of slices, spread the remaining blocks evenly between the first
284  * operations */
285  start_slice = slice * base_blocks_per_slice;
286  /* Add any extra blocks (one per slice) that have been added before this slice */
287  start_slice += FFMIN(slice, remainder_blocks);
288 
289  end_slice = start_slice + base_blocks_per_slice;
290  /* Add an extra block if there are still remainder blocks to be accounted for */
291  if (slice < remainder_blocks)
292  end_slice++;
293 
294  for (y = start_slice; y < end_slice; y++) {
295  uint8_t *p = frame->data[0] + y * frame->linesize[0] * TEXTURE_BLOCK_H;
296  int off = y * w_block;
297  for (x = 0; x < w_block; x++) {
298  ctx->tex_fun(p + x * 16, frame->linesize[0],
299  d + (off + x) * ctx->tex_rat);
300  }
301  }
302 
303  return 0;
304 }
305 
306 static int hap_decode(AVCodecContext *avctx, void *data,
307  int *got_frame, AVPacket *avpkt)
308 {
309  HapContext *ctx = avctx->priv_data;
310  ThreadFrame tframe;
311  int ret, i;
312 
313  bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
314 
315  /* Check for section header */
316  ret = hap_parse_frame_header(avctx);
317  if (ret < 0)
318  return ret;
319 
320  /* Get the output frame ready to receive data */
321  tframe.f = data;
322  ret = ff_thread_get_buffer(avctx, &tframe, 0);
323  if (ret < 0)
324  return ret;
325  ff_thread_finish_setup(avctx);
326 
327  /* Unpack the DXT texture */
328  if (hap_can_use_tex_in_place(ctx)) {
329  /* Only DXTC texture compression in a contiguous block */
330  ctx->tex_data = ctx->gbc.buffer;
331  } else {
332  /* Perform the second-stage decompression */
333  ret = av_reallocp(&ctx->tex_buf, ctx->tex_size);
334  if (ret < 0)
335  return ret;
336 
337  avctx->execute2(avctx, decompress_chunks_thread, NULL,
338  ctx->chunk_results, ctx->chunk_count);
339 
340  for (i = 0; i < ctx->chunk_count; i++) {
341  if (ctx->chunk_results[i] < 0)
342  return ctx->chunk_results[i];
343  }
344 
345  ctx->tex_data = ctx->tex_buf;
346  }
347 
348  /* Use the decompress function on the texture, one block per thread */
349  avctx->execute2(avctx, decompress_texture_thread, tframe.f, NULL, ctx->slice_count);
350 
351  /* Frame is ready to be output */
352  tframe.f->pict_type = AV_PICTURE_TYPE_I;
353  tframe.f->key_frame = 1;
354  *got_frame = 1;
355 
356  return avpkt->size;
357 }
358 
359 static av_cold int hap_init(AVCodecContext *avctx)
360 {
361  HapContext *ctx = avctx->priv_data;
362  const char *texture_name;
363  int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
364 
365  if (ret < 0) {
366  av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
367  avctx->width, avctx->height);
368  return ret;
369  }
370 
371  /* Since codec is based on 4x4 blocks, size is aligned to 4 */
372  avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
373  avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
374 
375  /* Technically only one mode has alpha, but 32 bits are easier to handle */
376  avctx->pix_fmt = AV_PIX_FMT_RGBA;
377 
378  ff_texturedsp_init(&ctx->dxtc);
379 
380  switch (avctx->codec_tag) {
381  case MKTAG('H','a','p','1'):
382  texture_name = "DXT1";
383  ctx->tex_rat = 8;
384  ctx->tex_fun = ctx->dxtc.dxt1_block;
385  break;
386  case MKTAG('H','a','p','5'):
387  texture_name = "DXT5";
388  ctx->tex_rat = 16;
389  ctx->tex_fun = ctx->dxtc.dxt5_block;
390  break;
391  case MKTAG('H','a','p','Y'):
392  texture_name = "DXT5-YCoCg-scaled";
393  ctx->tex_rat = 16;
394  ctx->tex_fun = ctx->dxtc.dxt5ys_block;
395  break;
396  default:
398  }
399 
400  av_log(avctx, AV_LOG_DEBUG, "%s texture\n", texture_name);
401 
402  ctx->slice_count = av_clip(avctx->thread_count, 1,
403  avctx->coded_height / TEXTURE_BLOCK_H);
404 
405  return 0;
406 }
407 
408 static av_cold int hap_close(AVCodecContext *avctx)
409 {
410  HapContext *ctx = avctx->priv_data;
411 
412  ff_hap_free_context(ctx);
413 
414  return 0;
415 }
416 
418  .name = "hap",
419  .long_name = NULL_IF_CONFIG_SMALL("Vidvox Hap decoder"),
420  .type = AVMEDIA_TYPE_VIDEO,
421  .id = AV_CODEC_ID_HAP,
422  .init = hap_init,
423  .decode = hap_decode,
424  .close = hap_close,
425  .priv_data_size = sizeof(HapContext),
428  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
430 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
Definition: hap.h:60
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int size
int(* dxt5_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: texturedsp.h:51
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
HapChunk * chunks
Definition: hap.h:70
static int hap_can_use_tex_in_place(HapContext *ctx)
Definition: hapdec.c:139
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1595
HapSectionType
Definition: hap.h:45
misc image utilities
AVFrame * f
Definition: thread.h:36
Texture block (4x4) module.
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:132
size_t compressed_size
Definition: hap.h:55
int compressed_offset
Definition: hap.h:54
AVCodec.
Definition: avcodec.h:3120
size_t uncompressed_size
Definition: hap.h:57
int64_t ff_snappy_peek_uncompressed_length(GetByteContext *gb)
Get the uncompressed length of an input buffer compressed using the Snappy algorithm.
Definition: snappy.c:131
#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
#define av_cold
Definition: attributes.h:66
static int hap_parse_decode_instructions(HapContext *ctx, int size)
Definition: hapdec.c:70
Multithreading support functions.
AVCodec ff_hap_decoder
Definition: hapdec.c:417
int ff_hap_set_chunk_count(HapContext *ctx, int count, int first_in_frame)
Definition: hap.c:28
#define TEXTURE_BLOCK_H
Definition: texturedsp.h:43
const char data[16]
Definition: mxf.c:70
TextureDSPContext dxtc
Definition: hap.h:63
uint8_t * data
Definition: avcodec.h:1346
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:140
const uint8_t * buffer
Definition: bytestream.h:33
static av_cold int hap_init(AVCodecContext *avctx)
Definition: hapdec.c:359
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
#define FFALIGN(x, a)
Definition: macros.h:48
GetByteContext gbc
Definition: hap.h:64
static int hap_decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: hapdec.c:306
int(* dxt5ys_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: texturedsp.h:53
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
int * chunk_results
Definition: hap.h:71
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:260
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:145
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:151
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:893
int ff_snappy_uncompress(GetByteContext *gb, uint8_t *buf, int64_t *size)
Decompress an input buffer using Snappy algorithm.
Definition: snappy.c:141
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:90
static av_cold int hap_close(AVCodecContext *avctx)
Definition: hapdec.c:408
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:223
av_cold void ff_texturedsp_init(TextureDSPContext *c)
Definition: texturedsp.c:598
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:201
int uncompressed_offset
Definition: hap.h:56
#define FFMIN(a, b)
Definition: common.h:66
int width
picture width / height.
Definition: avcodec.h:1580
static int parse_section_header(GetByteContext *gbc, int *section_size, enum HapSectionType *section_type)
Definition: hapdec.c:48
AVFormatContext * ctx
Definition: movenc.c:48
int tex_rat
Definition: hap.h:73
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2806
Snappy decompression.
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:897
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
Definition: avcodec.h:2866
NULL
Definition: eval.c:55
int(* tex_fun)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: hap.h:83
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
av_cold void ff_hap_free_context(HapContext *ctx)
Definition: hap.c:50
main external API structure.
Definition: avcodec.h:1409
const uint8_t * tex_data
Definition: hap.h:74
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
Definition: avcodec.h:1441
int chunk_count
Definition: hap.h:69
int(* dxt1_block)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
Definition: texturedsp.h:46
int coded_height
Definition: avcodec.h:1595
int slice_count
Definition: hap.h:80
static int decompress_chunks_thread(AVCodecContext *avctx, void *arg, int chunk_nb, int thread_nb)
Definition: hapdec.c:241
static int hap_parse_frame_header(AVCodecContext *avctx)
Definition: hapdec.c:152
static int decompress_texture_thread(AVCodecContext *avctx, void *arg, int slice, int thread_nb)
Definition: hapdec.c:269
uint8_t * tex_buf
Definition: hap.h:75
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
common internal api header.
size_t tex_size
Definition: hap.h:76
Definition: hap.h:52
void * priv_data
Definition: avcodec.h:1451
#define AVERROR_DECODER_NOT_FOUND
Decoder not found.
Definition: error.h:48
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:196
enum HapCompressor compressor
Definition: hap.h:53
#define MKTAG(a, b, c, d)
Definition: common.h:256
#define TEXTURE_BLOCK_W
Definition: texturedsp.h:42
This structure stores compressed data.
Definition: avcodec.h:1323
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:838