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.

374 lines
11KB

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