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.

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