You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

384 lines
12KB

  1. /*
  2. * Escape 124 Video Decoder
  3. * Copyright (C) 2008 Eli Friedman (eli.friedman@gmail.com)
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #define BITSTREAM_READER_LE
  22. #include "avcodec.h"
  23. #include "get_bits.h"
  24. #include "internal.h"
  25. typedef union MacroBlock {
  26. uint16_t pixels[4];
  27. uint32_t pixels32[2];
  28. } MacroBlock;
  29. typedef union SuperBlock {
  30. uint16_t pixels[64];
  31. uint32_t pixels32[32];
  32. } SuperBlock;
  33. typedef struct CodeBook {
  34. unsigned depth;
  35. unsigned size;
  36. MacroBlock* blocks;
  37. } CodeBook;
  38. typedef struct Escape124Context {
  39. AVFrame *frame;
  40. unsigned num_superblocks;
  41. CodeBook codebooks[3];
  42. } Escape124Context;
  43. static int can_safely_read(GetBitContext* gb, int bits) {
  44. return get_bits_left(gb) >= bits;
  45. }
  46. /**
  47. * Initialize the decoder
  48. * @param avctx decoder context
  49. * @return 0 success, negative on error
  50. */
  51. static av_cold int escape124_decode_init(AVCodecContext *avctx)
  52. {
  53. Escape124Context *s = avctx->priv_data;
  54. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  55. s->num_superblocks = ((unsigned)avctx->width / 8) *
  56. ((unsigned)avctx->height / 8);
  57. s->frame = av_frame_alloc();
  58. if (!s->frame)
  59. return AVERROR(ENOMEM);
  60. return 0;
  61. }
  62. static av_cold int escape124_decode_close(AVCodecContext *avctx)
  63. {
  64. unsigned i;
  65. Escape124Context *s = avctx->priv_data;
  66. for (i = 0; i < 3; i++)
  67. av_free(s->codebooks[i].blocks);
  68. av_frame_free(&s->frame);
  69. return 0;
  70. }
  71. static CodeBook unpack_codebook(GetBitContext* gb, unsigned depth,
  72. unsigned size)
  73. {
  74. unsigned i, j;
  75. CodeBook cb = { 0 };
  76. if (!can_safely_read(gb, size * 34))
  77. return cb;
  78. if (size >= INT_MAX / sizeof(MacroBlock))
  79. return cb;
  80. cb.blocks = av_malloc(size ? size * sizeof(MacroBlock) : 1);
  81. if (!cb.blocks)
  82. return cb;
  83. cb.depth = depth;
  84. cb.size = size;
  85. for (i = 0; i < size; i++) {
  86. unsigned mask_bits = get_bits(gb, 4);
  87. unsigned color0 = get_bits(gb, 15);
  88. unsigned color1 = get_bits(gb, 15);
  89. for (j = 0; j < 4; j++) {
  90. if (mask_bits & (1 << j))
  91. cb.blocks[i].pixels[j] = color1;
  92. else
  93. cb.blocks[i].pixels[j] = color0;
  94. }
  95. }
  96. return cb;
  97. }
  98. static unsigned decode_skip_count(GetBitContext* gb)
  99. {
  100. unsigned value;
  101. // This function reads a maximum of 23 bits,
  102. // which is within the padding space
  103. if (!can_safely_read(gb, 1))
  104. return -1;
  105. value = get_bits1(gb);
  106. if (!value)
  107. return value;
  108. value += get_bits(gb, 3);
  109. if (value != (1 + ((1 << 3) - 1)))
  110. return value;
  111. value += get_bits(gb, 7);
  112. if (value != (1 + ((1 << 3) - 1)) + ((1 << 7) - 1))
  113. return value;
  114. return value + get_bits(gb, 12);
  115. }
  116. static MacroBlock decode_macroblock(Escape124Context* s, GetBitContext* gb,
  117. int* codebook_index, int superblock_index)
  118. {
  119. // This function reads a maximum of 22 bits; the callers
  120. // guard this function appropriately
  121. unsigned block_index, depth;
  122. int value = get_bits1(gb);
  123. if (value) {
  124. static const char transitions[3][2] = { {2, 1}, {0, 2}, {1, 0} };
  125. value = get_bits1(gb);
  126. *codebook_index = transitions[*codebook_index][value];
  127. }
  128. depth = s->codebooks[*codebook_index].depth;
  129. // depth = 0 means that this shouldn't read any bits;
  130. // in theory, this is the same as get_bits(gb, 0), but
  131. // that doesn't actually work.
  132. block_index = get_bitsz(gb, depth);
  133. if (*codebook_index == 1) {
  134. block_index += superblock_index << s->codebooks[1].depth;
  135. }
  136. // This condition can occur with invalid bitstreams and
  137. // *codebook_index == 2
  138. if (block_index >= s->codebooks[*codebook_index].size)
  139. return (MacroBlock) { { 0 } };
  140. return s->codebooks[*codebook_index].blocks[block_index];
  141. }
  142. static void insert_mb_into_sb(SuperBlock* sb, MacroBlock mb, unsigned index) {
  143. // Formula: ((index / 4) * 16 + (index % 4) * 2) / 2
  144. uint32_t *dst = sb->pixels32 + index + (index & -4);
  145. // This technically violates C99 aliasing rules, but it should be safe.
  146. dst[0] = mb.pixels32[0];
  147. dst[4] = mb.pixels32[1];
  148. }
  149. static void copy_superblock(uint16_t* dest, unsigned dest_stride,
  150. uint16_t* src, unsigned src_stride)
  151. {
  152. unsigned y;
  153. if (src)
  154. for (y = 0; y < 8; y++)
  155. memcpy(dest + y * dest_stride, src + y * src_stride,
  156. sizeof(uint16_t) * 8);
  157. else
  158. for (y = 0; y < 8; y++)
  159. memset(dest + y * dest_stride, 0, sizeof(uint16_t) * 8);
  160. }
  161. static const uint16_t mask_matrix[] = {0x1, 0x2, 0x10, 0x20,
  162. 0x4, 0x8, 0x40, 0x80,
  163. 0x100, 0x200, 0x1000, 0x2000,
  164. 0x400, 0x800, 0x4000, 0x8000};
  165. static int escape124_decode_frame(AVCodecContext *avctx,
  166. void *data, int *got_frame,
  167. AVPacket *avpkt)
  168. {
  169. const uint8_t *buf = avpkt->data;
  170. int buf_size = avpkt->size;
  171. Escape124Context *s = avctx->priv_data;
  172. AVFrame *frame = data;
  173. GetBitContext gb;
  174. unsigned frame_flags, frame_size;
  175. unsigned i;
  176. unsigned superblock_index, cb_index = 1,
  177. superblock_col_index = 0,
  178. superblocks_per_row = avctx->width / 8, skip = -1;
  179. uint16_t* old_frame_data, *new_frame_data;
  180. unsigned old_stride, new_stride;
  181. int ret;
  182. init_get_bits(&gb, buf, buf_size * 8);
  183. // This call also guards the potential depth reads for the
  184. // codebook unpacking.
  185. if (!can_safely_read(&gb, 64))
  186. return -1;
  187. frame_flags = get_bits_long(&gb, 32);
  188. frame_size = get_bits_long(&gb, 32);
  189. // Leave last frame unchanged
  190. // FIXME: Is this necessary? I haven't seen it in any real samples
  191. if (!(frame_flags & 0x114) || !(frame_flags & 0x7800000)) {
  192. if (!s->frame->data[0])
  193. return AVERROR_INVALIDDATA;
  194. av_log(NULL, AV_LOG_DEBUG, "Skipping frame\n");
  195. *got_frame = 1;
  196. if ((ret = av_frame_ref(frame, s->frame)) < 0)
  197. return ret;
  198. return frame_size;
  199. }
  200. for (i = 0; i < 3; i++) {
  201. if (frame_flags & (1 << (17 + i))) {
  202. unsigned cb_depth, cb_size;
  203. if (i == 2) {
  204. // This codebook can be cut off at places other than
  205. // powers of 2, leaving some of the entries undefined.
  206. cb_size = get_bits_long(&gb, 20);
  207. cb_depth = av_log2(cb_size - 1) + 1;
  208. } else {
  209. cb_depth = get_bits(&gb, 4);
  210. if (i == 0) {
  211. // This is the most basic codebook: pow(2,depth) entries
  212. // for a depth-length key
  213. cb_size = 1 << cb_depth;
  214. } else {
  215. // This codebook varies per superblock
  216. // FIXME: I don't think this handles integer overflow
  217. // properly
  218. cb_size = s->num_superblocks << cb_depth;
  219. }
  220. }
  221. av_free(s->codebooks[i].blocks);
  222. s->codebooks[i] = unpack_codebook(&gb, cb_depth, cb_size);
  223. if (!s->codebooks[i].blocks)
  224. return -1;
  225. }
  226. }
  227. if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0) {
  228. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  229. return ret;
  230. }
  231. new_frame_data = (uint16_t*)frame->data[0];
  232. new_stride = frame->linesize[0] / 2;
  233. old_frame_data = (uint16_t*)s->frame->data[0];
  234. old_stride = s->frame->linesize[0] / 2;
  235. for (superblock_index = 0; superblock_index < s->num_superblocks;
  236. superblock_index++) {
  237. MacroBlock mb;
  238. SuperBlock sb;
  239. unsigned multi_mask = 0;
  240. if (skip == -1) {
  241. // Note that this call will make us skip the rest of the blocks
  242. // if the frame prematurely ends
  243. skip = decode_skip_count(&gb);
  244. }
  245. if (skip) {
  246. copy_superblock(new_frame_data, new_stride,
  247. old_frame_data, old_stride);
  248. } else {
  249. copy_superblock(sb.pixels, 8,
  250. old_frame_data, old_stride);
  251. while (can_safely_read(&gb, 1) && !get_bits1(&gb)) {
  252. unsigned mask;
  253. mb = decode_macroblock(s, &gb, &cb_index, superblock_index);
  254. mask = get_bits(&gb, 16);
  255. multi_mask |= mask;
  256. for (i = 0; i < 16; i++) {
  257. if (mask & mask_matrix[i]) {
  258. insert_mb_into_sb(&sb, mb, i);
  259. }
  260. }
  261. }
  262. if (can_safely_read(&gb, 1) && !get_bits1(&gb)) {
  263. unsigned inv_mask = get_bits(&gb, 4);
  264. for (i = 0; i < 4; i++) {
  265. if (inv_mask & (1 << i)) {
  266. multi_mask ^= 0xF << i*4;
  267. } else {
  268. multi_mask ^= get_bits(&gb, 4) << i*4;
  269. }
  270. }
  271. for (i = 0; i < 16; i++) {
  272. if (multi_mask & mask_matrix[i]) {
  273. if (!can_safely_read(&gb, 1))
  274. break;
  275. mb = decode_macroblock(s, &gb, &cb_index,
  276. superblock_index);
  277. insert_mb_into_sb(&sb, mb, i);
  278. }
  279. }
  280. } else if (frame_flags & (1 << 16)) {
  281. while (can_safely_read(&gb, 1) && !get_bits1(&gb)) {
  282. mb = decode_macroblock(s, &gb, &cb_index, superblock_index);
  283. insert_mb_into_sb(&sb, mb, get_bits(&gb, 4));
  284. }
  285. }
  286. copy_superblock(new_frame_data, new_stride, sb.pixels, 8);
  287. }
  288. superblock_col_index++;
  289. new_frame_data += 8;
  290. if (old_frame_data)
  291. old_frame_data += 8;
  292. if (superblock_col_index == superblocks_per_row) {
  293. new_frame_data += new_stride * 8 - superblocks_per_row * 8;
  294. if (old_frame_data)
  295. old_frame_data += old_stride * 8 - superblocks_per_row * 8;
  296. superblock_col_index = 0;
  297. }
  298. skip--;
  299. }
  300. av_log(NULL, AV_LOG_DEBUG,
  301. "Escape sizes: %i, %i, %i\n",
  302. frame_size, buf_size, get_bits_count(&gb) / 8);
  303. av_frame_unref(s->frame);
  304. if ((ret = av_frame_ref(s->frame, frame)) < 0)
  305. return ret;
  306. *got_frame = 1;
  307. return frame_size;
  308. }
  309. AVCodec ff_escape124_decoder = {
  310. .name = "escape124",
  311. .long_name = NULL_IF_CONFIG_SMALL("Escape 124"),
  312. .type = AVMEDIA_TYPE_VIDEO,
  313. .id = AV_CODEC_ID_ESCAPE124,
  314. .priv_data_size = sizeof(Escape124Context),
  315. .init = escape124_decode_init,
  316. .close = escape124_decode_close,
  317. .decode = escape124_decode_frame,
  318. .capabilities = AV_CODEC_CAP_DR1,
  319. };