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.

360 lines
10KB

  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 "libavutil/attributes.h"
  22. #include "libavutil/mem.h"
  23. #include "avcodec.h"
  24. #define BITSTREAM_READER_LE
  25. #include "get_bits.h"
  26. #include "internal.h"
  27. typedef struct Escape130Context {
  28. uint8_t *old_y_avg;
  29. uint8_t *new_y, *old_y;
  30. uint8_t *new_u, *old_u;
  31. uint8_t *new_v, *old_v;
  32. uint8_t *buf1, *buf2;
  33. int linesize[3];
  34. } Escape130Context;
  35. static const uint8_t offset_table[] = { 2, 4, 10, 20 };
  36. static const int8_t sign_table[64][4] = {
  37. { 0, 0, 0, 0 },
  38. { -1, 1, 0, 0 },
  39. { 1, -1, 0, 0 },
  40. { -1, 0, 1, 0 },
  41. { -1, 1, 1, 0 },
  42. { 0, -1, 1, 0 },
  43. { 1, -1, 1, 0 },
  44. { -1, -1, 1, 0 },
  45. { 1, 0, -1, 0 },
  46. { 0, 1, -1, 0 },
  47. { 1, 1, -1, 0 },
  48. { -1, 1, -1, 0 },
  49. { 1, -1, -1, 0 },
  50. { -1, 0, 0, 1 },
  51. { -1, 1, 0, 1 },
  52. { 0, -1, 0, 1 },
  53. { 0, 0, 0, 0 },
  54. { 1, -1, 0, 1 },
  55. { -1, -1, 0, 1 },
  56. { -1, 0, 1, 1 },
  57. { -1, 1, 1, 1 },
  58. { 0, -1, 1, 1 },
  59. { 1, -1, 1, 1 },
  60. { -1, -1, 1, 1 },
  61. { 0, 0, -1, 1 },
  62. { 1, 0, -1, 1 },
  63. { -1, 0, -1, 1 },
  64. { 0, 1, -1, 1 },
  65. { 1, 1, -1, 1 },
  66. { -1, 1, -1, 1 },
  67. { 0, -1, -1, 1 },
  68. { 1, -1, -1, 1 },
  69. { 0, 0, 0, 0 },
  70. { -1, -1, -1, 1 },
  71. { 1, 0, 0, -1 },
  72. { 0, 1, 0, -1 },
  73. { 1, 1, 0, -1 },
  74. { -1, 1, 0, -1 },
  75. { 1, -1, 0, -1 },
  76. { 0, 0, 1, -1 },
  77. { 1, 0, 1, -1 },
  78. { -1, 0, 1, -1 },
  79. { 0, 1, 1, -1 },
  80. { 1, 1, 1, -1 },
  81. { -1, 1, 1, -1 },
  82. { 0, -1, 1, -1 },
  83. { 1, -1, 1, -1 },
  84. { -1, -1, 1, -1 },
  85. { 0, 0, 0, 0 },
  86. { 1, 0, -1, -1 },
  87. { 0, 1, -1, -1 },
  88. { 1, 1, -1, -1 },
  89. { -1, 1, -1, -1 },
  90. { 1, -1, -1, -1 }
  91. };
  92. static const int8_t luma_adjust[] = { -4, -3, -2, -1, 1, 2, 3, 4 };
  93. static const int8_t chroma_adjust[2][8] = {
  94. { 1, 1, 0, -1, -1, -1, 0, 1 },
  95. { 0, 1, 1, 1, 0, -1, -1, -1 }
  96. };
  97. const uint8_t chroma_vals[] = {
  98. 20, 28, 36, 44, 52, 60, 68, 76,
  99. 84, 92, 100, 106, 112, 116, 120, 124,
  100. 128, 132, 136, 140, 144, 150, 156, 164,
  101. 172, 180, 188, 196, 204, 212, 220, 228
  102. };
  103. static av_cold int escape130_decode_init(AVCodecContext *avctx)
  104. {
  105. Escape130Context *s = avctx->priv_data;
  106. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  107. if ((avctx->width & 1) || (avctx->height & 1)) {
  108. av_log(avctx, AV_LOG_ERROR,
  109. "Dimensions should be a multiple of two.\n");
  110. return AVERROR_INVALIDDATA;
  111. }
  112. s->old_y_avg = av_malloc(avctx->width * avctx->height / 4);
  113. s->buf1 = av_malloc(avctx->width * avctx->height * 3 / 2);
  114. s->buf2 = av_malloc(avctx->width * avctx->height * 3 / 2);
  115. if (!s->old_y_avg || !s->buf1 || !s->buf2) {
  116. av_freep(&s->old_y_avg);
  117. av_freep(&s->buf1);
  118. av_freep(&s->buf2);
  119. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
  120. return AVERROR(ENOMEM);
  121. }
  122. s->linesize[0] = avctx->width;
  123. s->linesize[1] =
  124. s->linesize[2] = avctx->width / 2;
  125. s->new_y = s->buf1;
  126. s->new_u = s->new_y + avctx->width * avctx->height;
  127. s->new_v = s->new_u + avctx->width * avctx->height / 4;
  128. s->old_y = s->buf2;
  129. s->old_u = s->old_y + avctx->width * avctx->height;
  130. s->old_v = s->old_u + avctx->width * avctx->height / 4;
  131. memset(s->old_y, 0, avctx->width * avctx->height);
  132. memset(s->old_u, 0x10, avctx->width * avctx->height / 4);
  133. memset(s->old_v, 0x10, avctx->width * avctx->height / 4);
  134. return 0;
  135. }
  136. static av_cold int escape130_decode_close(AVCodecContext *avctx)
  137. {
  138. Escape130Context *s = avctx->priv_data;
  139. av_freep(&s->old_y_avg);
  140. av_freep(&s->buf1);
  141. av_freep(&s->buf2);
  142. return 0;
  143. }
  144. static int decode_skip_count(GetBitContext* gb)
  145. {
  146. int value;
  147. if (get_bits_left(gb) < 1+3)
  148. return -1;
  149. value = get_bits1(gb);
  150. if (value)
  151. return 0;
  152. value = get_bits(gb, 3);
  153. if (value)
  154. return value;
  155. value = get_bits(gb, 8);
  156. if (value)
  157. return value + 7;
  158. value = get_bits(gb, 15);
  159. if (value)
  160. return value + 262;
  161. return -1;
  162. }
  163. static int escape130_decode_frame(AVCodecContext *avctx, void *data,
  164. int *got_frame, AVPacket *avpkt)
  165. {
  166. const uint8_t *buf = avpkt->data;
  167. int buf_size = avpkt->size;
  168. Escape130Context *s = avctx->priv_data;
  169. AVFrame *pic = data;
  170. GetBitContext gb;
  171. int ret;
  172. uint8_t *old_y, *old_cb, *old_cr,
  173. *new_y, *new_cb, *new_cr;
  174. uint8_t *dstY, *dstU, *dstV;
  175. unsigned old_y_stride, old_cb_stride, old_cr_stride,
  176. new_y_stride, new_cb_stride, new_cr_stride;
  177. unsigned total_blocks = avctx->width * avctx->height / 4,
  178. block_index, block_x = 0;
  179. unsigned y[4] = { 0 }, cb = 0x10, cr = 0x10;
  180. int skip = -1, y_avg = 0, i, j;
  181. uint8_t *ya = s->old_y_avg;
  182. // first 16 bytes are header; no useful information in here
  183. if (buf_size <= 16) {
  184. av_log(avctx, AV_LOG_ERROR, "Insufficient frame data\n");
  185. return AVERROR_INVALIDDATA;
  186. }
  187. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  188. return ret;
  189. init_get_bits(&gb, buf + 16, (buf_size - 16) * 8);
  190. new_y = s->new_y;
  191. new_cb = s->new_u;
  192. new_cr = s->new_v;
  193. new_y_stride = s->linesize[0];
  194. new_cb_stride = s->linesize[1];
  195. new_cr_stride = s->linesize[2];
  196. old_y = s->old_y;
  197. old_cb = s->old_u;
  198. old_cr = s->old_v;
  199. old_y_stride = s->linesize[0];
  200. old_cb_stride = s->linesize[1];
  201. old_cr_stride = s->linesize[2];
  202. for (block_index = 0; block_index < total_blocks; block_index++) {
  203. // Note that this call will make us skip the rest of the blocks
  204. // if the frame ends prematurely.
  205. if (skip == -1)
  206. skip = decode_skip_count(&gb);
  207. if (skip == -1) {
  208. av_log(avctx, AV_LOG_ERROR, "Error decoding skip value\n");
  209. return AVERROR_INVALIDDATA;
  210. }
  211. if (skip) {
  212. y[0] = old_y[0];
  213. y[1] = old_y[1];
  214. y[2] = old_y[old_y_stride];
  215. y[3] = old_y[old_y_stride + 1];
  216. y_avg = ya[0];
  217. cb = old_cb[0];
  218. cr = old_cr[0];
  219. } else {
  220. if (get_bits1(&gb)) {
  221. unsigned sign_selector = get_bits(&gb, 6);
  222. unsigned difference_selector = get_bits(&gb, 2);
  223. y_avg = 2 * get_bits(&gb, 5);
  224. for (i = 0; i < 4; i++) {
  225. y[i] = av_clip(y_avg + offset_table[difference_selector] *
  226. sign_table[sign_selector][i], 0, 63);
  227. }
  228. } else if (get_bits1(&gb)) {
  229. if (get_bits1(&gb)) {
  230. y_avg = get_bits(&gb, 6);
  231. } else {
  232. unsigned adjust_index = get_bits(&gb, 3);
  233. y_avg = (y_avg + luma_adjust[adjust_index]) & 63;
  234. }
  235. for (i = 0; i < 4; i++)
  236. y[i] = y_avg;
  237. }
  238. if (get_bits1(&gb)) {
  239. if (get_bits1(&gb)) {
  240. cb = get_bits(&gb, 5);
  241. cr = get_bits(&gb, 5);
  242. } else {
  243. unsigned adjust_index = get_bits(&gb, 3);
  244. cb = (cb + chroma_adjust[0][adjust_index]) & 31;
  245. cr = (cr + chroma_adjust[1][adjust_index]) & 31;
  246. }
  247. }
  248. }
  249. *ya++ = y_avg;
  250. new_y[0] = y[0];
  251. new_y[1] = y[1];
  252. new_y[new_y_stride] = y[2];
  253. new_y[new_y_stride + 1] = y[3];
  254. *new_cb = cb;
  255. *new_cr = cr;
  256. old_y += 2;
  257. old_cb++;
  258. old_cr++;
  259. new_y += 2;
  260. new_cb++;
  261. new_cr++;
  262. block_x++;
  263. if (block_x * 2 == avctx->width) {
  264. block_x = 0;
  265. old_y += old_y_stride * 2 - avctx->width;
  266. old_cb += old_cb_stride - avctx->width / 2;
  267. old_cr += old_cr_stride - avctx->width / 2;
  268. new_y += new_y_stride * 2 - avctx->width;
  269. new_cb += new_cb_stride - avctx->width / 2;
  270. new_cr += new_cr_stride - avctx->width / 2;
  271. }
  272. skip--;
  273. }
  274. new_y = s->new_y;
  275. new_cb = s->new_u;
  276. new_cr = s->new_v;
  277. dstY = pic->data[0];
  278. dstU = pic->data[1];
  279. dstV = pic->data[2];
  280. for (j = 0; j < avctx->height; j++) {
  281. for (i = 0; i < avctx->width; i++)
  282. dstY[i] = new_y[i] << 2;
  283. dstY += pic->linesize[0];
  284. new_y += new_y_stride;
  285. }
  286. for (j = 0; j < avctx->height / 2; j++) {
  287. for (i = 0; i < avctx->width / 2; i++) {
  288. dstU[i] = chroma_vals[new_cb[i]];
  289. dstV[i] = chroma_vals[new_cr[i]];
  290. }
  291. dstU += pic->linesize[1];
  292. dstV += pic->linesize[2];
  293. new_cb += new_cb_stride;
  294. new_cr += new_cr_stride;
  295. }
  296. av_dlog(avctx, "Frame data: provided %d bytes, used %d bytes\n",
  297. buf_size, get_bits_count(&gb) >> 3);
  298. FFSWAP(uint8_t*, s->old_y, s->new_y);
  299. FFSWAP(uint8_t*, s->old_u, s->new_u);
  300. FFSWAP(uint8_t*, s->old_v, s->new_v);
  301. *got_frame = 1;
  302. return buf_size;
  303. }
  304. AVCodec ff_escape130_decoder = {
  305. .name = "escape130",
  306. .type = AVMEDIA_TYPE_VIDEO,
  307. .id = AV_CODEC_ID_ESCAPE130,
  308. .priv_data_size = sizeof(Escape130Context),
  309. .init = escape130_decode_init,
  310. .close = escape130_decode_close,
  311. .decode = escape130_decode_frame,
  312. .capabilities = CODEC_CAP_DR1,
  313. .long_name = NULL_IF_CONFIG_SMALL("Escape 130"),
  314. };