escape124.c 12 KB

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