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.

319 lines
9.8KB

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