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.

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