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.

310 lines
9.6KB

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