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.

324 lines
10.0KB

  1. /*
  2. * Escape 130 Video Decoder
  3. * Copyright (C) 2008 Eli Friedman (eli.friedman <at> 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 "get_bits.h"
  24. typedef struct Escape130Context {
  25. AVFrame frame;
  26. uint8_t *bases;
  27. } Escape130Context;
  28. static int can_safely_read(GetBitContext* gb, int bits) {
  29. return get_bits_count(gb) + bits <= gb->size_in_bits;
  30. }
  31. /**
  32. * Initialize the decoder
  33. * @param avctx decoder context
  34. * @return 0 success, negative on error
  35. */
  36. static av_cold int escape130_decode_init(AVCodecContext *avctx)
  37. {
  38. Escape130Context *s = avctx->priv_data;
  39. avctx->pix_fmt = PIX_FMT_YUV420P;
  40. if((avctx->width&1) || (avctx->height&1)){
  41. av_log(avctx, AV_LOG_ERROR, "Dimensions are not a multiple of the block size\n");
  42. return AVERROR(EINVAL);
  43. }
  44. s->bases= av_malloc(avctx->width * avctx->height /4);
  45. return 0;
  46. }
  47. static av_cold int escape130_decode_close(AVCodecContext *avctx)
  48. {
  49. Escape130Context *s = avctx->priv_data;
  50. if (s->frame.data[0])
  51. avctx->release_buffer(avctx, &s->frame);
  52. av_freep(&s->bases);
  53. return 0;
  54. }
  55. static unsigned decode_skip_count(GetBitContext* gb) {
  56. unsigned value;
  57. // This function reads a maximum of 27 bits,
  58. // which is within the padding space
  59. if (!can_safely_read(gb, 1+3))
  60. return -1;
  61. value = get_bits1(gb);
  62. if (value)
  63. return 0;
  64. value = get_bits(gb, 3);
  65. if (value)
  66. return value;
  67. value = get_bits(gb, 8);
  68. if (value)
  69. return value + 7;
  70. value = get_bits(gb, 15);
  71. if (value)
  72. return value + 262;
  73. return -1;
  74. }
  75. /**
  76. * Decode a single frame
  77. * @param avctx decoder context
  78. * @param data decoded frame
  79. * @param data_size size of the decoded frame
  80. * @param buf input buffer
  81. * @param buf_size input buffer size
  82. * @return 0 success, -1 on error
  83. */
  84. static int escape130_decode_frame(AVCodecContext *avctx,
  85. void *data, int *data_size,
  86. AVPacket *avpkt)
  87. {
  88. const uint8_t *buf = avpkt->data;
  89. int buf_size = avpkt->size;
  90. Escape130Context *s = avctx->priv_data;
  91. GetBitContext gb;
  92. unsigned i;
  93. uint8_t *old_y, *old_cb, *old_cr,
  94. *new_y, *new_cb, *new_cr;
  95. unsigned old_y_stride, old_cb_stride, old_cr_stride,
  96. new_y_stride, new_cb_stride, new_cr_stride;
  97. unsigned total_blocks = avctx->width * avctx->height / 4,
  98. block_index, row_index = 0;
  99. unsigned y[4] = {0}, cb = 16, cr = 16;
  100. unsigned skip = -1;
  101. unsigned y_base = 0;
  102. uint8_t *yb= s->bases;
  103. AVFrame new_frame = { { 0 } };
  104. init_get_bits(&gb, buf, buf_size * 8);
  105. if (!can_safely_read(&gb, 128))
  106. return -1;
  107. // Header; no useful information in here
  108. skip_bits_long(&gb, 128);
  109. new_frame.reference = 3;
  110. if (avctx->get_buffer(avctx, &new_frame)) {
  111. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  112. return -1;
  113. }
  114. new_y = new_frame.data[0];
  115. new_cb = new_frame.data[1];
  116. new_cr = new_frame.data[2];
  117. new_y_stride = new_frame.linesize[0];
  118. new_cb_stride = new_frame.linesize[1];
  119. new_cr_stride = new_frame.linesize[2];
  120. old_y = s->frame.data[0];
  121. old_cb = s->frame.data[1];
  122. old_cr = s->frame.data[2];
  123. old_y_stride = s->frame.linesize[0];
  124. old_cb_stride = s->frame.linesize[1];
  125. old_cr_stride = s->frame.linesize[2];
  126. av_log(NULL, AV_LOG_DEBUG,
  127. "Strides: %i, %i\n",
  128. new_y_stride, new_cb_stride);
  129. for (block_index = 0; block_index < total_blocks; block_index++) {
  130. // Note that this call will make us skip the rest of the blocks
  131. // if the frame prematurely ends
  132. if (skip == -1)
  133. skip = decode_skip_count(&gb);
  134. if (skip) {
  135. if (old_y) {
  136. y[0] = old_y[0] / 4;
  137. y[1] = old_y[1] / 4;
  138. y[2] = old_y[old_y_stride] / 4;
  139. y[3] = old_y[old_y_stride+1] / 4;
  140. y_base= yb[0];
  141. cb = old_cb[0] / 8;
  142. cr = old_cr[0] / 8;
  143. } else {
  144. y_base=y[0] = y[1] = y[2] = y[3] = 0;
  145. cb = cr = 16;
  146. }
  147. } else {
  148. if (get_bits1(&gb)) {
  149. static const uint8_t offset_table[] = {2, 4, 10, 20};
  150. static const int8_t sign_table[64][4] =
  151. { {0, 0, 0, 0},
  152. {-1, 1, 0, 0},
  153. {1, -1, 0, 0},
  154. {-1, 0, 1, 0},
  155. {-1, 1, 1, 0},
  156. {0, -1, 1, 0},
  157. {1, -1, 1, 0},
  158. {-1, -1, 1, 0},
  159. {1, 0, -1, 0},
  160. {0, 1, -1, 0},
  161. {1, 1, -1, 0},
  162. {-1, 1, -1, 0},
  163. {1, -1, -1, 0},
  164. {-1, 0, 0, 1},
  165. {-1, 1, 0, 1},
  166. {0, -1, 0, 1},
  167. {0, 0, 0, 0},
  168. {1, -1, 0, 1},
  169. {-1, -1, 0, 1},
  170. {-1, 0, 1, 1},
  171. {-1, 1, 1, 1},
  172. {0, -1, 1, 1},
  173. {1, -1, 1, 1},
  174. {-1, -1, 1, 1},
  175. {0, 0, -1, 1},
  176. {1, 0, -1, 1},
  177. {-1, 0, -1, 1},
  178. {0, 1, -1, 1},
  179. {1, 1, -1, 1},
  180. {-1, 1, -1, 1},
  181. {0, -1, -1, 1},
  182. {1, -1, -1, 1},
  183. {0, 0, 0, 0},
  184. {-1, -1, -1, 1},
  185. {1, 0, 0, -1},
  186. {0, 1, 0, -1},
  187. {1, 1, 0, -1},
  188. {-1, 1, 0, -1},
  189. {1, -1, 0, -1},
  190. {0, 0, 1, -1},
  191. {1, 0, 1, -1},
  192. {-1, 0, 1, -1},
  193. {0, 1, 1, -1},
  194. {1, 1, 1, -1},
  195. {-1, 1, 1, -1},
  196. {0, -1, 1, -1},
  197. {1, -1, 1, -1},
  198. {-1, -1, 1, -1},
  199. {0, 0, 0, 0},
  200. {1, 0, -1, -1},
  201. {0, 1, -1, -1},
  202. {1, 1, -1, -1},
  203. {-1, 1, -1, -1},
  204. {1, -1, -1, -1} };
  205. unsigned sign_selector = get_bits(&gb, 6);
  206. unsigned difference_selector = get_bits(&gb, 2);
  207. y_base = 2 * get_bits(&gb, 5);
  208. for (i = 0; i < 4; i++) {
  209. y[i] = av_clip((int)y_base + offset_table[difference_selector] *
  210. sign_table[sign_selector][i], 0, 63);
  211. }
  212. } else if (get_bits1(&gb)) {
  213. if (get_bits1(&gb)) {
  214. y_base = get_bits(&gb, 6);
  215. } else {
  216. unsigned adjust_index = get_bits(&gb, 3);
  217. static const int8_t adjust[] = {-4, -3, -2, -1, 1, 2, 3, 4};
  218. y_base = (y_base + adjust[adjust_index]) & 63;
  219. }
  220. for (i = 0; i < 4; i++)
  221. y[i] = y_base;
  222. }
  223. if (get_bits1(&gb)) {
  224. if (get_bits1(&gb)) {
  225. cb = get_bits(&gb, 5);
  226. cr = get_bits(&gb, 5);
  227. } else {
  228. unsigned adjust_index = get_bits(&gb, 3);
  229. static const int8_t adjust[2][8] =
  230. { { 1, 1, 0, -1, -1, -1, 0, 1 },
  231. { 0, 1, 1, 1, 0, -1, -1, -1 } };
  232. cb = (cb + adjust[0][adjust_index]) & 31;
  233. cr = (cr + adjust[1][adjust_index]) & 31;
  234. }
  235. }
  236. }
  237. *yb++= y_base;
  238. new_y[0] = y[0] * 4;
  239. new_y[1] = y[1] * 4;
  240. new_y[new_y_stride] = y[2] * 4;
  241. new_y[new_y_stride + 1] = y[3] * 4;
  242. *new_cb = cb * 8;
  243. *new_cr = cr * 8;
  244. if (old_y)
  245. old_y += 2, old_cb++, old_cr++;
  246. new_y += 2, new_cb++, new_cr++;
  247. row_index++;
  248. if (avctx->width / 2 == row_index) {
  249. row_index = 0;
  250. if (old_y) {
  251. old_y += old_y_stride * 2 - avctx->width;
  252. old_cb += old_cb_stride - avctx->width / 2;
  253. old_cr += old_cr_stride - avctx->width / 2;
  254. }
  255. new_y += new_y_stride * 2 - avctx->width;
  256. new_cb += new_cb_stride - avctx->width / 2;
  257. new_cr += new_cr_stride - avctx->width / 2;
  258. }
  259. skip--;
  260. }
  261. av_log(NULL, AV_LOG_DEBUG,
  262. "Escape sizes: %i, %i\n",
  263. buf_size, get_bits_count(&gb) / 8);
  264. if (s->frame.data[0])
  265. avctx->release_buffer(avctx, &s->frame);
  266. *(AVFrame*)data = s->frame = new_frame;
  267. *data_size = sizeof(AVFrame);
  268. return buf_size;
  269. }
  270. AVCodec ff_escape130_decoder = {
  271. .name = "escape130",
  272. .type = AVMEDIA_TYPE_VIDEO,
  273. .id = CODEC_ID_ESCAPE130,
  274. .priv_data_size = sizeof(Escape130Context),
  275. .init = escape130_decode_init,
  276. .close = escape130_decode_close,
  277. .decode = escape130_decode_frame,
  278. .capabilities = CODEC_CAP_DR1,
  279. .long_name = NULL_IF_CONFIG_SMALL("Escape 130"),
  280. };