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.

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