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.

358 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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. #define BITSTREAM_READER_LE
  24. #include "avcodec.h"
  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. static 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. value = get_bits1(gb);
  148. if (value)
  149. return 0;
  150. value = get_bits(gb, 3);
  151. if (value)
  152. return value;
  153. value = get_bits(gb, 8);
  154. if (value)
  155. return value + 7;
  156. value = get_bits(gb, 15);
  157. if (value)
  158. return value + 262;
  159. return -1;
  160. }
  161. static int escape130_decode_frame(AVCodecContext *avctx, void *data,
  162. int *got_frame, AVPacket *avpkt)
  163. {
  164. const uint8_t *buf = avpkt->data;
  165. int buf_size = avpkt->size;
  166. Escape130Context *s = avctx->priv_data;
  167. AVFrame *pic = data;
  168. GetBitContext gb;
  169. int ret;
  170. uint8_t *old_y, *old_cb, *old_cr,
  171. *new_y, *new_cb, *new_cr;
  172. uint8_t *dstY, *dstU, *dstV;
  173. unsigned old_y_stride, old_cb_stride, old_cr_stride,
  174. new_y_stride, new_cb_stride, new_cr_stride;
  175. unsigned total_blocks = avctx->width * avctx->height / 4,
  176. block_index, block_x = 0;
  177. unsigned y[4] = { 0 }, cb = 0x10, cr = 0x10;
  178. int skip = -1, y_avg = 0, i, j;
  179. uint8_t *ya = s->old_y_avg;
  180. // first 16 bytes are header; no useful information in here
  181. if (buf_size <= 16) {
  182. av_log(avctx, AV_LOG_ERROR, "Insufficient frame data\n");
  183. return AVERROR_INVALIDDATA;
  184. }
  185. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  186. return ret;
  187. init_get_bits(&gb, buf + 16, (buf_size - 16) * 8);
  188. new_y = s->new_y;
  189. new_cb = s->new_u;
  190. new_cr = s->new_v;
  191. new_y_stride = s->linesize[0];
  192. new_cb_stride = s->linesize[1];
  193. new_cr_stride = s->linesize[2];
  194. old_y = s->old_y;
  195. old_cb = s->old_u;
  196. old_cr = s->old_v;
  197. old_y_stride = s->linesize[0];
  198. old_cb_stride = s->linesize[1];
  199. old_cr_stride = s->linesize[2];
  200. for (block_index = 0; block_index < total_blocks; block_index++) {
  201. // Note that this call will make us skip the rest of the blocks
  202. // if the frame ends prematurely.
  203. if (skip == -1)
  204. skip = decode_skip_count(&gb);
  205. if (skip == -1) {
  206. av_log(avctx, AV_LOG_ERROR, "Error decoding skip value\n");
  207. return AVERROR_INVALIDDATA;
  208. }
  209. if (skip) {
  210. y[0] = old_y[0];
  211. y[1] = old_y[1];
  212. y[2] = old_y[old_y_stride];
  213. y[3] = old_y[old_y_stride + 1];
  214. y_avg = ya[0];
  215. cb = old_cb[0];
  216. cr = old_cr[0];
  217. } else {
  218. if (get_bits1(&gb)) {
  219. unsigned sign_selector = get_bits(&gb, 6);
  220. unsigned difference_selector = get_bits(&gb, 2);
  221. y_avg = 2 * get_bits(&gb, 5);
  222. for (i = 0; i < 4; i++) {
  223. y[i] = av_clip(y_avg + offset_table[difference_selector] *
  224. sign_table[sign_selector][i], 0, 63);
  225. }
  226. } else if (get_bits1(&gb)) {
  227. if (get_bits1(&gb)) {
  228. y_avg = get_bits(&gb, 6);
  229. } else {
  230. unsigned adjust_index = get_bits(&gb, 3);
  231. y_avg = (y_avg + luma_adjust[adjust_index]) & 63;
  232. }
  233. for (i = 0; i < 4; i++)
  234. y[i] = y_avg;
  235. }
  236. if (get_bits1(&gb)) {
  237. if (get_bits1(&gb)) {
  238. cb = get_bits(&gb, 5);
  239. cr = get_bits(&gb, 5);
  240. } else {
  241. unsigned adjust_index = get_bits(&gb, 3);
  242. cb = (cb + chroma_adjust[0][adjust_index]) & 31;
  243. cr = (cr + chroma_adjust[1][adjust_index]) & 31;
  244. }
  245. }
  246. }
  247. *ya++ = y_avg;
  248. new_y[0] = y[0];
  249. new_y[1] = y[1];
  250. new_y[new_y_stride] = y[2];
  251. new_y[new_y_stride + 1] = y[3];
  252. *new_cb = cb;
  253. *new_cr = cr;
  254. old_y += 2;
  255. old_cb++;
  256. old_cr++;
  257. new_y += 2;
  258. new_cb++;
  259. new_cr++;
  260. block_x++;
  261. if (block_x * 2 == avctx->width) {
  262. block_x = 0;
  263. old_y += old_y_stride * 2 - avctx->width;
  264. old_cb += old_cb_stride - avctx->width / 2;
  265. old_cr += old_cr_stride - avctx->width / 2;
  266. new_y += new_y_stride * 2 - avctx->width;
  267. new_cb += new_cb_stride - avctx->width / 2;
  268. new_cr += new_cr_stride - avctx->width / 2;
  269. }
  270. skip--;
  271. }
  272. new_y = s->new_y;
  273. new_cb = s->new_u;
  274. new_cr = s->new_v;
  275. dstY = pic->data[0];
  276. dstU = pic->data[1];
  277. dstV = pic->data[2];
  278. for (j = 0; j < avctx->height; j++) {
  279. for (i = 0; i < avctx->width; i++)
  280. dstY[i] = new_y[i] << 2;
  281. dstY += pic->linesize[0];
  282. new_y += new_y_stride;
  283. }
  284. for (j = 0; j < avctx->height / 2; j++) {
  285. for (i = 0; i < avctx->width / 2; i++) {
  286. dstU[i] = chroma_vals[new_cb[i]];
  287. dstV[i] = chroma_vals[new_cr[i]];
  288. }
  289. dstU += pic->linesize[1];
  290. dstV += pic->linesize[2];
  291. new_cb += new_cb_stride;
  292. new_cr += new_cr_stride;
  293. }
  294. ff_dlog(avctx, "Frame data: provided %d bytes, used %d bytes\n",
  295. buf_size, get_bits_count(&gb) >> 3);
  296. FFSWAP(uint8_t*, s->old_y, s->new_y);
  297. FFSWAP(uint8_t*, s->old_u, s->new_u);
  298. FFSWAP(uint8_t*, s->old_v, s->new_v);
  299. *got_frame = 1;
  300. return buf_size;
  301. }
  302. AVCodec ff_escape130_decoder = {
  303. .name = "escape130",
  304. .long_name = NULL_IF_CONFIG_SMALL("Escape 130"),
  305. .type = AVMEDIA_TYPE_VIDEO,
  306. .id = AV_CODEC_ID_ESCAPE130,
  307. .priv_data_size = sizeof(Escape130Context),
  308. .init = escape130_decode_init,
  309. .close = escape130_decode_close,
  310. .decode = escape130_decode_frame,
  311. .capabilities = AV_CODEC_CAP_DR1,
  312. };