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.

320 lines
9.9KB

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