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.

480 lines
16KB

  1. /*
  2. * NotchLC decoder
  3. * Copyright (c) 2020 Paul B Mahol
  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 <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #define BITSTREAM_READER_LE
  25. #include "libavutil/intreadwrite.h"
  26. #include "avcodec.h"
  27. #include "bytestream.h"
  28. #include "get_bits.h"
  29. #include "internal.h"
  30. #include "lzf.h"
  31. #include "thread.h"
  32. typedef struct NotchLCContext {
  33. unsigned compressed_size;
  34. unsigned format;
  35. uint8_t *uncompressed_buffer;
  36. unsigned uncompressed_size;
  37. uint8_t *lzf_buffer;
  38. int64_t lzf_size;
  39. unsigned texture_size_x;
  40. unsigned texture_size_y;
  41. unsigned y_data_row_offsets;
  42. unsigned uv_offset_data_offset;
  43. unsigned y_control_data_offset;
  44. unsigned a_control_word_offset;
  45. unsigned y_data_offset;
  46. unsigned uv_data_offset;
  47. unsigned y_data_size;
  48. unsigned uv_count_size;
  49. unsigned uv_count_offset;
  50. unsigned a_count_size;
  51. unsigned data_end;
  52. GetByteContext gb;
  53. PutByteContext pb;
  54. } NotchLCContext;
  55. static av_cold int decode_init(AVCodecContext *avctx)
  56. {
  57. avctx->pix_fmt = AV_PIX_FMT_YUV444P12;
  58. avctx->color_range = AVCOL_RANGE_JPEG;
  59. avctx->colorspace = AVCOL_SPC_RGB;
  60. avctx->color_primaries = AVCOL_PRI_BT709;
  61. avctx->color_trc = AVCOL_TRC_IEC61966_2_1;
  62. return 0;
  63. }
  64. #define HISTORY_SIZE (64 * 1024)
  65. static int lz4_decompress(AVCodecContext *avctx,
  66. GetByteContext *gb,
  67. PutByteContext *pb)
  68. {
  69. unsigned reference_pos, match_length, delta, pos = 0;
  70. uint8_t history[64 * 1024];
  71. while (bytestream2_get_bytes_left(gb) > 0) {
  72. uint8_t token = bytestream2_get_byte(gb);
  73. unsigned num_literals = token >> 4;
  74. if (num_literals == 15) {
  75. unsigned char current;
  76. do {
  77. current = bytestream2_get_byte(gb);
  78. num_literals += current;
  79. } while (current == 255);
  80. }
  81. if (pos + num_literals < HISTORY_SIZE) {
  82. bytestream2_get_buffer(gb, history + pos, num_literals);
  83. pos += num_literals;
  84. } else {
  85. while (num_literals-- > 0) {
  86. history[pos++] = bytestream2_get_byte(gb);
  87. if (pos == HISTORY_SIZE) {
  88. bytestream2_put_buffer(pb, history, HISTORY_SIZE);
  89. pos = 0;
  90. }
  91. }
  92. }
  93. if (bytestream2_get_bytes_left(gb) <= 0)
  94. break;
  95. delta = bytestream2_get_byte(gb);
  96. delta |= (unsigned)bytestream2_get_byte(gb) << 8;
  97. if (delta == 0)
  98. return 0;
  99. match_length = 4 + (token & 0x0F);
  100. if (match_length == 4 + 0x0F) {
  101. uint8_t current;
  102. do {
  103. current = bytestream2_get_byte(gb);
  104. match_length += current;
  105. } while (current == 255);
  106. }
  107. reference_pos = (pos >= delta) ? (pos - delta) : (HISTORY_SIZE + pos - delta);
  108. if (pos + match_length < HISTORY_SIZE && reference_pos + match_length < HISTORY_SIZE) {
  109. if (pos >= reference_pos + match_length || reference_pos >= pos + match_length) {
  110. memcpy(history + pos, history + reference_pos, match_length);
  111. pos += match_length;
  112. } else {
  113. while (match_length-- > 0)
  114. history[pos++] = history[reference_pos++];
  115. }
  116. } else {
  117. while (match_length-- > 0) {
  118. history[pos++] = history[reference_pos++];
  119. if (pos == HISTORY_SIZE) {
  120. bytestream2_put_buffer(pb, history, HISTORY_SIZE);
  121. pos = 0;
  122. }
  123. reference_pos %= HISTORY_SIZE;
  124. }
  125. }
  126. }
  127. bytestream2_put_buffer(pb, history, pos);
  128. return bytestream2_tell_p(pb);
  129. }
  130. static int decode_blocks(AVCodecContext *avctx, AVFrame *p, ThreadFrame *frame,
  131. unsigned uncompressed_size)
  132. {
  133. NotchLCContext *s = avctx->priv_data;
  134. GetByteContext rgb, dgb, *gb = &s->gb;
  135. GetBitContext bit;
  136. int ylinesize, ulinesize, vlinesize, alinesize;
  137. uint16_t *dsty, *dstu, *dstv, *dsta;
  138. int ret;
  139. s->texture_size_x = bytestream2_get_le32(gb);
  140. s->texture_size_y = bytestream2_get_le32(gb);
  141. ret = ff_set_dimensions(avctx, s->texture_size_x, s->texture_size_y);
  142. if (ret < 0)
  143. return ret;
  144. s->uv_offset_data_offset = bytestream2_get_le32(gb);
  145. if (s->uv_offset_data_offset >= UINT_MAX / 4)
  146. return AVERROR_INVALIDDATA;
  147. s->uv_offset_data_offset *= 4;
  148. if (s->uv_offset_data_offset >= uncompressed_size)
  149. return AVERROR_INVALIDDATA;
  150. s->y_control_data_offset = bytestream2_get_le32(gb);
  151. if (s->y_control_data_offset >= UINT_MAX / 4)
  152. return AVERROR_INVALIDDATA;
  153. s->y_control_data_offset *= 4;
  154. if (s->y_control_data_offset >= uncompressed_size)
  155. return AVERROR_INVALIDDATA;
  156. s->a_control_word_offset = bytestream2_get_le32(gb);
  157. if (s->a_control_word_offset >= UINT_MAX / 4)
  158. return AVERROR_INVALIDDATA;
  159. s->a_control_word_offset *= 4;
  160. if (s->a_control_word_offset >= uncompressed_size)
  161. return AVERROR_INVALIDDATA;
  162. s->uv_data_offset = bytestream2_get_le32(gb);
  163. if (s->uv_data_offset >= UINT_MAX / 4)
  164. return AVERROR_INVALIDDATA;
  165. s->uv_data_offset *= 4;
  166. if (s->uv_data_offset >= uncompressed_size)
  167. return AVERROR_INVALIDDATA;
  168. s->y_data_size = bytestream2_get_le32(gb);
  169. if (s->y_data_size >= UINT_MAX / 4)
  170. return AVERROR_INVALIDDATA;
  171. s->uv_count_size = bytestream2_get_le32(gb);
  172. if (s->uv_count_size >= UINT_MAX / 4)
  173. return AVERROR_INVALIDDATA;
  174. s->uv_count_size *= 4;
  175. if (s->uv_count_size >= uncompressed_size)
  176. return AVERROR_INVALIDDATA;
  177. s->a_count_size = bytestream2_get_le32(gb);
  178. if (s->a_count_size >= UINT_MAX / 4)
  179. return AVERROR_INVALIDDATA;
  180. s->a_count_size *= 4;
  181. if (s->a_count_size >= uncompressed_size)
  182. return AVERROR_INVALIDDATA;
  183. s->data_end = bytestream2_get_le32(gb);
  184. if (s->data_end > uncompressed_size)
  185. return AVERROR_INVALIDDATA;
  186. s->y_data_row_offsets = bytestream2_tell(gb);
  187. if (s->data_end <= s->y_data_size)
  188. return AVERROR_INVALIDDATA;
  189. s->y_data_offset = s->data_end - s->y_data_size;
  190. if (s->y_data_offset <= s->uv_count_size)
  191. return AVERROR_INVALIDDATA;
  192. s->uv_count_offset = s->y_data_offset - s->uv_count_size;
  193. if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0)
  194. return ret;
  195. rgb = *gb;
  196. dgb = *gb;
  197. bytestream2_seek(&rgb, s->y_data_row_offsets, SEEK_SET);
  198. bytestream2_seek(gb, s->y_control_data_offset, SEEK_SET);
  199. dsty = (uint16_t *)p->data[0];
  200. dsta = (uint16_t *)p->data[3];
  201. ylinesize = p->linesize[0] / 2;
  202. alinesize = p->linesize[3] / 2;
  203. for (int y = 0; y < avctx->height; y += 4) {
  204. const unsigned row_offset = bytestream2_get_le32(&rgb);
  205. bytestream2_seek(&dgb, s->y_data_offset + row_offset, SEEK_SET);
  206. init_get_bits8(&bit, dgb.buffer, bytestream2_get_bytes_left(&dgb));
  207. for (int x = 0; x < avctx->width; x += 4) {
  208. unsigned item = bytestream2_get_le32(gb);
  209. unsigned y_min = item & 4095;
  210. unsigned y_max = (item >> 12) & 4095;
  211. unsigned y_diff = y_max - y_min;
  212. unsigned control[4];
  213. control[0] = (item >> 24) & 3;
  214. control[1] = (item >> 26) & 3;
  215. control[2] = (item >> 28) & 3;
  216. control[3] = (item >> 30) & 3;
  217. for (int i = 0; i < 4; i++) {
  218. const int nb_bits = control[i] + 1;
  219. const int div = (1 << nb_bits) - 1;
  220. const int add = div - 1;
  221. dsty[x + i * ylinesize + 0] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
  222. dsty[x + i * ylinesize + 1] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
  223. dsty[x + i * ylinesize + 2] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
  224. dsty[x + i * ylinesize + 3] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
  225. }
  226. }
  227. dsty += 4 * ylinesize;
  228. dsta += 4 * alinesize;
  229. }
  230. rgb = *gb;
  231. dgb = *gb;
  232. bytestream2_seek(&rgb, s->uv_offset_data_offset, SEEK_SET);
  233. bytestream2_seek(gb, s->a_control_word_offset, SEEK_SET);
  234. dstu = (uint16_t *)p->data[1];
  235. dstv = (uint16_t *)p->data[2];
  236. ulinesize = p->linesize[1] / 2;
  237. vlinesize = p->linesize[2] / 2;
  238. for (int y = 0; y < avctx->height; y += 16) {
  239. for (int x = 0; x < avctx->width; x += 16) {
  240. unsigned offset = bytestream2_get_le32(&rgb) * 4;
  241. int u[16][16] = { 0 }, v[16][16] = { 0 };
  242. int u0, v0, u1, v1, udif, vdif;
  243. unsigned escape, is8x8, loc;
  244. bytestream2_seek(&dgb, s->uv_data_offset + offset, SEEK_SET);
  245. is8x8 = bytestream2_get_le16(&dgb);
  246. escape = bytestream2_get_le16(&dgb);
  247. if (escape == 0 && is8x8 == 0) {
  248. u0 = bytestream2_get_byte(&dgb);
  249. v0 = bytestream2_get_byte(&dgb);
  250. u1 = bytestream2_get_byte(&dgb);
  251. v1 = bytestream2_get_byte(&dgb);
  252. loc = bytestream2_get_le32(&dgb);
  253. u0 = (u0 << 4) | (u0 & 0xF);
  254. v0 = (v0 << 4) | (v0 & 0xF);
  255. u1 = (u1 << 4) | (u1 & 0xF);
  256. v1 = (v1 << 4) | (v1 & 0xF);
  257. udif = u1 - u0;
  258. vdif = v1 - v0;
  259. for (int i = 0; i < 16; i += 4) {
  260. for (int j = 0; j < 16; j += 4) {
  261. for (int ii = 0; ii < 4; ii++) {
  262. for (int jj = 0; jj < 4; jj++) {
  263. u[i + ii][j + jj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
  264. v[i + ii][j + jj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
  265. }
  266. }
  267. loc >>= 2;
  268. }
  269. }
  270. } else {
  271. for (int i = 0; i < 16; i += 8) {
  272. for (int j = 0; j < 16; j += 8) {
  273. if (is8x8 & 1) {
  274. u0 = bytestream2_get_byte(&dgb);
  275. v0 = bytestream2_get_byte(&dgb);
  276. u1 = bytestream2_get_byte(&dgb);
  277. v1 = bytestream2_get_byte(&dgb);
  278. loc = bytestream2_get_le32(&dgb);
  279. u0 = (u0 << 4) | (u0 & 0xF);
  280. v0 = (v0 << 4) | (v0 & 0xF);
  281. u1 = (u1 << 4) | (u1 & 0xF);
  282. v1 = (v1 << 4) | (v1 & 0xF);
  283. udif = u1 - u0;
  284. vdif = v1 - v0;
  285. for (int ii = 0; ii < 8; ii += 2) {
  286. for (int jj = 0; jj < 8; jj += 2) {
  287. for (int iii = 0; iii < 2; iii++) {
  288. for (int jjj = 0; jjj < 2; jjj++) {
  289. u[i + ii + iii][j + jj + jjj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
  290. v[i + ii + iii][j + jj + jjj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
  291. }
  292. }
  293. loc >>= 2;
  294. }
  295. }
  296. } else if (escape) {
  297. for (int ii = 0; ii < 8; ii += 4) {
  298. for (int jj = 0; jj < 8; jj += 4) {
  299. u0 = bytestream2_get_byte(&dgb);
  300. v0 = bytestream2_get_byte(&dgb);
  301. u1 = bytestream2_get_byte(&dgb);
  302. v1 = bytestream2_get_byte(&dgb);
  303. loc = bytestream2_get_le32(&dgb);
  304. u0 = (u0 << 4) | (u0 & 0xF);
  305. v0 = (v0 << 4) | (v0 & 0xF);
  306. u1 = (u1 << 4) | (u1 & 0xF);
  307. v1 = (v1 << 4) | (v1 & 0xF);
  308. udif = u1 - u0;
  309. vdif = v1 - v0;
  310. for (int iii = 0; iii < 4; iii++) {
  311. for (int jjj = 0; jjj < 4; jjj++) {
  312. u[i + ii + iii][j + jj + jjj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
  313. v[i + ii + iii][j + jj + jjj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
  314. loc >>= 2;
  315. }
  316. }
  317. }
  318. }
  319. }
  320. is8x8 >>= 1;
  321. }
  322. }
  323. }
  324. for (int i = 0; i < 16; i++) {
  325. for (int j = 0; j < 16; j++) {
  326. dstu[x + i * ulinesize + j] = u[i][j];
  327. dstv[x + i * vlinesize + j] = v[i][j];
  328. }
  329. }
  330. }
  331. dstu += 16 * ulinesize;
  332. dstv += 16 * vlinesize;
  333. }
  334. return 0;
  335. }
  336. static int decode_frame(AVCodecContext *avctx,
  337. void *data, int *got_frame,
  338. AVPacket *avpkt)
  339. {
  340. NotchLCContext *s = avctx->priv_data;
  341. ThreadFrame frame = { .f = data };
  342. GetByteContext *gb = &s->gb;
  343. PutByteContext *pb = &s->pb;
  344. unsigned uncompressed_size;
  345. AVFrame *p = data;
  346. int ret;
  347. if (avpkt->size <= 40)
  348. return AVERROR_INVALIDDATA;
  349. bytestream2_init(gb, avpkt->data, avpkt->size);
  350. if (bytestream2_get_le32(gb) != MKBETAG('N','L','C','1'))
  351. return AVERROR_INVALIDDATA;
  352. uncompressed_size = bytestream2_get_le32(gb);
  353. s->compressed_size = bytestream2_get_le32(gb);
  354. s->format = bytestream2_get_le32(gb);
  355. if (s->format > 2)
  356. return AVERROR_PATCHWELCOME;
  357. if (s->format == 0) {
  358. ret = ff_lzf_uncompress(gb, &s->lzf_buffer, &s->lzf_size);
  359. if (ret < 0)
  360. return ret;
  361. if (uncompressed_size > s->lzf_size)
  362. return AVERROR_INVALIDDATA;
  363. bytestream2_init(gb, s->lzf_buffer, uncompressed_size);
  364. } else if (s->format == 1) {
  365. av_fast_padded_malloc(&s->uncompressed_buffer, &s->uncompressed_size,
  366. uncompressed_size);
  367. if (!s->uncompressed_buffer)
  368. return AVERROR(ENOMEM);
  369. bytestream2_init_writer(pb, s->uncompressed_buffer, s->uncompressed_size);
  370. ret = lz4_decompress(avctx, gb, pb);
  371. if (ret != uncompressed_size)
  372. return AVERROR_INVALIDDATA;
  373. bytestream2_init(gb, s->uncompressed_buffer, uncompressed_size);
  374. }
  375. ret = decode_blocks(avctx, p, &frame, uncompressed_size);
  376. if (ret < 0)
  377. return ret;
  378. p->pict_type = AV_PICTURE_TYPE_I;
  379. p->key_frame = 1;
  380. *got_frame = 1;
  381. return avpkt->size;
  382. }
  383. static av_cold int decode_end(AVCodecContext *avctx)
  384. {
  385. NotchLCContext *s = avctx->priv_data;
  386. av_freep(&s->uncompressed_buffer);
  387. s->uncompressed_size = 0;
  388. av_freep(&s->lzf_buffer);
  389. s->lzf_size = 0;
  390. return 0;
  391. }
  392. AVCodec ff_notchlc_decoder = {
  393. .name = "notchlc",
  394. .long_name = NULL_IF_CONFIG_SMALL("NotchLC"),
  395. .type = AVMEDIA_TYPE_VIDEO,
  396. .id = AV_CODEC_ID_NOTCHLC,
  397. .priv_data_size = sizeof(NotchLCContext),
  398. .init = decode_init,
  399. .close = decode_end,
  400. .decode = decode_frame,
  401. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  402. };