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.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 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. avcodec_get_frame_defaults(&s->frame);
  39. if((avctx->width&1) || (avctx->height&1)){
  40. av_log(avctx, AV_LOG_ERROR, "Dimensions are not a multiple of the block size\n");
  41. return AVERROR(EINVAL);
  42. }
  43. s->bases= av_malloc(avctx->width * avctx->height /4);
  44. return 0;
  45. }
  46. static av_cold int escape130_decode_close(AVCodecContext *avctx)
  47. {
  48. Escape130Context *s = avctx->priv_data;
  49. av_frame_unref(&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. int ret;
  92. uint8_t *old_y, *old_cb, *old_cr,
  93. *new_y, *new_cb, *new_cr;
  94. unsigned old_y_stride, old_cb_stride, old_cr_stride,
  95. new_y_stride, new_cb_stride, new_cr_stride;
  96. unsigned total_blocks = avctx->width * avctx->height / 4,
  97. block_index, row_index = 0;
  98. unsigned y[4] = {0}, cb = 16, cr = 16;
  99. unsigned skip = -1;
  100. unsigned y_base = 0;
  101. uint8_t *yb= s->bases;
  102. AVFrame *frame = data;
  103. init_get_bits(&gb, buf, buf_size * 8);
  104. if (get_bits_left(&gb) < 128)
  105. return -1;
  106. // Header; no useful information in here
  107. skip_bits_long(&gb, 128);
  108. if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
  109. return ret;
  110. new_y = frame->data[0];
  111. new_cb = frame->data[1];
  112. new_cr = frame->data[2];
  113. new_y_stride = frame->linesize[0];
  114. new_cb_stride = frame->linesize[1];
  115. new_cr_stride = 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(avctx, 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. static const uint8_t offset_table[] = {2, 4, 10, 20};
  146. static const int8_t sign_table[64][4] =
  147. { {0, 0, 0, 0},
  148. {-1, 1, 0, 0},
  149. {1, -1, 0, 0},
  150. {-1, 0, 1, 0},
  151. {-1, 1, 1, 0},
  152. {0, -1, 1, 0},
  153. {1, -1, 1, 0},
  154. {-1, -1, 1, 0},
  155. {1, 0, -1, 0},
  156. {0, 1, -1, 0},
  157. {1, 1, -1, 0},
  158. {-1, 1, -1, 0},
  159. {1, -1, -1, 0},
  160. {-1, 0, 0, 1},
  161. {-1, 1, 0, 1},
  162. {0, -1, 0, 1},
  163. {0, 0, 0, 0},
  164. {1, -1, 0, 1},
  165. {-1, -1, 0, 1},
  166. {-1, 0, 1, 1},
  167. {-1, 1, 1, 1},
  168. {0, -1, 1, 1},
  169. {1, -1, 1, 1},
  170. {-1, -1, 1, 1},
  171. {0, 0, -1, 1},
  172. {1, 0, -1, 1},
  173. {-1, 0, -1, 1},
  174. {0, 1, -1, 1},
  175. {1, 1, -1, 1},
  176. {-1, 1, -1, 1},
  177. {0, -1, -1, 1},
  178. {1, -1, -1, 1},
  179. {0, 0, 0, 0},
  180. {-1, -1, -1, 1},
  181. {1, 0, 0, -1},
  182. {0, 1, 0, -1},
  183. {1, 1, 0, -1},
  184. {-1, 1, 0, -1},
  185. {1, -1, 0, -1},
  186. {0, 0, 1, -1},
  187. {1, 0, 1, -1},
  188. {-1, 0, 1, -1},
  189. {0, 1, 1, -1},
  190. {1, 1, 1, -1},
  191. {-1, 1, 1, -1},
  192. {0, -1, 1, -1},
  193. {1, -1, 1, -1},
  194. {-1, -1, 1, -1},
  195. {0, 0, 0, 0},
  196. {1, 0, -1, -1},
  197. {0, 1, -1, -1},
  198. {1, 1, -1, -1},
  199. {-1, 1, -1, -1},
  200. {1, -1, -1, -1} };
  201. unsigned sign_selector = get_bits(&gb, 6);
  202. unsigned difference_selector = get_bits(&gb, 2);
  203. y_base = 2 * get_bits(&gb, 5);
  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(avctx, AV_LOG_DEBUG,
  258. "Escape sizes: %i, %i\n",
  259. buf_size, get_bits_count(&gb) / 8);
  260. av_frame_unref(&s->frame);
  261. if ((ret = av_frame_ref(&s->frame, frame)) < 0)
  262. return ret;
  263. *got_frame = 1;
  264. return buf_size;
  265. }
  266. AVCodec ff_escape130_decoder = {
  267. .name = "escape130",
  268. .type = AVMEDIA_TYPE_VIDEO,
  269. .id = AV_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. };