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.

548 lines
19KB

  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 a_data_offset;
  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_YUVA444P12;
  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_le16(gb);
  96. if (delta == 0)
  97. return 0;
  98. match_length = 4 + (token & 0x0F);
  99. if (match_length == 4 + 0x0F) {
  100. uint8_t current;
  101. do {
  102. current = bytestream2_get_byte(gb);
  103. match_length += current;
  104. } while (current == 255);
  105. }
  106. reference_pos = (pos >= delta) ? (pos - delta) : (HISTORY_SIZE + pos - delta);
  107. if (pos + match_length < HISTORY_SIZE && reference_pos + match_length < HISTORY_SIZE) {
  108. if (pos >= reference_pos + match_length || reference_pos >= pos + match_length) {
  109. memcpy(history + pos, history + reference_pos, match_length);
  110. pos += match_length;
  111. } else {
  112. while (match_length-- > 0)
  113. history[pos++] = history[reference_pos++];
  114. }
  115. } else {
  116. while (match_length-- > 0) {
  117. history[pos++] = history[reference_pos++];
  118. if (pos == HISTORY_SIZE) {
  119. bytestream2_put_buffer(pb, history, HISTORY_SIZE);
  120. pos = 0;
  121. }
  122. reference_pos %= HISTORY_SIZE;
  123. }
  124. }
  125. }
  126. bytestream2_put_buffer(pb, history, pos);
  127. return bytestream2_tell_p(pb);
  128. }
  129. static int decode_blocks(AVCodecContext *avctx, AVFrame *p, ThreadFrame *frame,
  130. unsigned uncompressed_size)
  131. {
  132. NotchLCContext *s = avctx->priv_data;
  133. GetByteContext rgb, dgb, *gb = &s->gb;
  134. GetBitContext bit;
  135. int ylinesize, ulinesize, vlinesize, alinesize;
  136. uint16_t *dsty, *dstu, *dstv, *dsta;
  137. int ret;
  138. s->texture_size_x = bytestream2_get_le32(gb);
  139. s->texture_size_y = bytestream2_get_le32(gb);
  140. ret = ff_set_dimensions(avctx, s->texture_size_x, s->texture_size_y);
  141. if (ret < 0)
  142. return ret;
  143. s->uv_offset_data_offset = bytestream2_get_le32(gb);
  144. if (s->uv_offset_data_offset >= UINT_MAX / 4)
  145. return AVERROR_INVALIDDATA;
  146. s->uv_offset_data_offset *= 4;
  147. if (s->uv_offset_data_offset >= uncompressed_size)
  148. return AVERROR_INVALIDDATA;
  149. s->y_control_data_offset = bytestream2_get_le32(gb);
  150. if (s->y_control_data_offset >= UINT_MAX / 4)
  151. return AVERROR_INVALIDDATA;
  152. s->y_control_data_offset *= 4;
  153. if (s->y_control_data_offset >= uncompressed_size)
  154. return AVERROR_INVALIDDATA;
  155. s->a_control_word_offset = bytestream2_get_le32(gb);
  156. if (s->a_control_word_offset >= UINT_MAX / 4)
  157. return AVERROR_INVALIDDATA;
  158. s->a_control_word_offset *= 4;
  159. if (s->a_control_word_offset >= uncompressed_size)
  160. return AVERROR_INVALIDDATA;
  161. s->uv_data_offset = bytestream2_get_le32(gb);
  162. if (s->uv_data_offset >= UINT_MAX / 4)
  163. return AVERROR_INVALIDDATA;
  164. s->uv_data_offset *= 4;
  165. if (s->uv_data_offset >= uncompressed_size)
  166. return AVERROR_INVALIDDATA;
  167. s->y_data_size = bytestream2_get_le32(gb);
  168. if (s->y_data_size >= UINT_MAX / 4)
  169. return AVERROR_INVALIDDATA;
  170. s->a_data_offset = bytestream2_get_le32(gb);
  171. if (s->a_data_offset >= UINT_MAX / 4)
  172. return AVERROR_INVALIDDATA;
  173. s->a_data_offset *= 4;
  174. if (s->a_data_offset >= uncompressed_size)
  175. return AVERROR_INVALIDDATA;
  176. s->a_count_size = bytestream2_get_le32(gb);
  177. if (s->a_count_size >= UINT_MAX / 4)
  178. return AVERROR_INVALIDDATA;
  179. s->a_count_size *= 4;
  180. if (s->a_count_size >= uncompressed_size)
  181. return AVERROR_INVALIDDATA;
  182. s->data_end = bytestream2_get_le32(gb);
  183. if (s->data_end > uncompressed_size)
  184. return AVERROR_INVALIDDATA;
  185. s->y_data_row_offsets = bytestream2_tell(gb);
  186. if (s->data_end <= s->y_data_size)
  187. return AVERROR_INVALIDDATA;
  188. s->y_data_offset = s->data_end - s->y_data_size;
  189. if (s->y_data_offset <= s->a_data_offset)
  190. return AVERROR_INVALIDDATA;
  191. s->uv_count_offset = s->y_data_offset - s->a_data_offset;
  192. if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0)
  193. return ret;
  194. rgb = *gb;
  195. dgb = *gb;
  196. bytestream2_seek(&rgb, s->y_data_row_offsets, SEEK_SET);
  197. bytestream2_seek(gb, s->y_control_data_offset, SEEK_SET);
  198. if (bytestream2_get_bytes_left(gb) < (avctx->height + 3) / 4 * ((avctx->width + 3) / 4) * 4)
  199. return AVERROR_INVALIDDATA;
  200. dsty = (uint16_t *)p->data[0];
  201. dsta = (uint16_t *)p->data[3];
  202. ylinesize = p->linesize[0] / 2;
  203. alinesize = p->linesize[3] / 2;
  204. for (int y = 0; y < avctx->height; y += 4) {
  205. const unsigned row_offset = bytestream2_get_le32(&rgb);
  206. bytestream2_seek(&dgb, s->y_data_offset + row_offset, SEEK_SET);
  207. init_get_bits8(&bit, dgb.buffer, bytestream2_get_bytes_left(&dgb));
  208. for (int x = 0; x < avctx->width; x += 4) {
  209. unsigned item = bytestream2_get_le32(gb);
  210. unsigned y_min = item & 4095;
  211. unsigned y_max = (item >> 12) & 4095;
  212. unsigned y_diff = y_max - y_min;
  213. unsigned control[4];
  214. control[0] = (item >> 24) & 3;
  215. control[1] = (item >> 26) & 3;
  216. control[2] = (item >> 28) & 3;
  217. control[3] = (item >> 30) & 3;
  218. for (int i = 0; i < 4; i++) {
  219. const int nb_bits = control[i] + 1;
  220. const int div = (1 << nb_bits) - 1;
  221. const int add = div - 1;
  222. dsty[x + i * ylinesize + 0] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
  223. dsty[x + i * ylinesize + 1] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
  224. dsty[x + i * ylinesize + 2] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
  225. dsty[x + i * ylinesize + 3] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
  226. }
  227. }
  228. dsty += 4 * ylinesize;
  229. }
  230. rgb = *gb;
  231. dgb = *gb;
  232. bytestream2_seek(gb, s->a_control_word_offset, SEEK_SET);
  233. if (s->uv_count_offset == s->a_control_word_offset) {
  234. for (int y = 0; y < avctx->height; y++) {
  235. for (int x = 0; x < avctx->width; x++)
  236. dsta[x] = 4095;
  237. dsta += alinesize;
  238. }
  239. } else {
  240. if (bytestream2_get_bytes_left(gb) < (avctx->height + 15) / 16 * ((avctx->width + 15) / 16) * 8)
  241. return AVERROR_INVALIDDATA;
  242. for (int y = 0; y < avctx->height; y += 16) {
  243. for (int x = 0; x < avctx->width; x += 16) {
  244. unsigned m = bytestream2_get_le32(gb);
  245. unsigned offset = bytestream2_get_le32(gb);
  246. unsigned alpha0, alpha1;
  247. uint64_t control;
  248. if (offset >= UINT_MAX / 4)
  249. return AVERROR_INVALIDDATA;
  250. offset = offset * 4 + s->uv_data_offset + s->a_data_offset;
  251. if (offset >= s->data_end)
  252. return AVERROR_INVALIDDATA;
  253. bytestream2_seek(&dgb, offset, SEEK_SET);
  254. control = bytestream2_get_le64(&dgb);
  255. alpha0 = control & 0xFF;
  256. alpha1 = (control >> 8) & 0xFF;
  257. control = control >> 16;
  258. for (int by = 0; by < 4; by++) {
  259. for (int bx = 0; bx < 4; bx++) {
  260. switch (m & 3) {
  261. case 0:
  262. for (int i = 0; i < 4; i++) {
  263. for (int j = 0; j < 4; j++) {
  264. dsta[x + (i + by * 4) * alinesize + bx * 4 + j] = 0;
  265. }
  266. }
  267. break;
  268. case 1:
  269. for (int i = 0; i < 4; i++) {
  270. for (int j = 0; j < 4; j++) {
  271. dsta[x + (i + by * 4) * alinesize + bx * 4 + j] = 4095;
  272. }
  273. }
  274. break;
  275. case 2:
  276. for (int i = 0; i < 4; i++) {
  277. for (int j = 0; j < 4; j++) {
  278. dsta[x + (i + by * 4) * alinesize + bx * 4 + j] = (alpha0 + (alpha1 - alpha0) * (control & 7)) << 4;
  279. }
  280. }
  281. break;
  282. default:
  283. return AVERROR_INVALIDDATA;
  284. }
  285. control >>= 3;
  286. m >>= 2;
  287. }
  288. }
  289. }
  290. dsta += 16 * alinesize;
  291. }
  292. }
  293. bytestream2_seek(&rgb, s->uv_offset_data_offset, SEEK_SET);
  294. dstu = (uint16_t *)p->data[1];
  295. dstv = (uint16_t *)p->data[2];
  296. ulinesize = p->linesize[1] / 2;
  297. vlinesize = p->linesize[2] / 2;
  298. for (int y = 0; y < avctx->height; y += 16) {
  299. for (int x = 0; x < avctx->width; x += 16) {
  300. unsigned offset = bytestream2_get_le32(&rgb) * 4;
  301. int u[16][16] = { 0 }, v[16][16] = { 0 };
  302. int u0, v0, u1, v1, udif, vdif;
  303. unsigned escape, is8x8, loc;
  304. bytestream2_seek(&dgb, s->uv_data_offset + offset, SEEK_SET);
  305. is8x8 = bytestream2_get_le16(&dgb);
  306. escape = bytestream2_get_le16(&dgb);
  307. if (escape == 0 && is8x8 == 0) {
  308. u0 = bytestream2_get_byte(&dgb);
  309. v0 = bytestream2_get_byte(&dgb);
  310. u1 = bytestream2_get_byte(&dgb);
  311. v1 = bytestream2_get_byte(&dgb);
  312. loc = bytestream2_get_le32(&dgb);
  313. u0 = (u0 << 4) | (u0 & 0xF);
  314. v0 = (v0 << 4) | (v0 & 0xF);
  315. u1 = (u1 << 4) | (u1 & 0xF);
  316. v1 = (v1 << 4) | (v1 & 0xF);
  317. udif = u1 - u0;
  318. vdif = v1 - v0;
  319. for (int i = 0; i < 16; i += 4) {
  320. for (int j = 0; j < 16; j += 4) {
  321. for (int ii = 0; ii < 4; ii++) {
  322. for (int jj = 0; jj < 4; jj++) {
  323. u[i + ii][j + jj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
  324. v[i + ii][j + jj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
  325. }
  326. }
  327. loc >>= 2;
  328. }
  329. }
  330. } else {
  331. for (int i = 0; i < 16; i += 8) {
  332. for (int j = 0; j < 16; j += 8) {
  333. if (is8x8 & 1) {
  334. u0 = bytestream2_get_byte(&dgb);
  335. v0 = bytestream2_get_byte(&dgb);
  336. u1 = bytestream2_get_byte(&dgb);
  337. v1 = bytestream2_get_byte(&dgb);
  338. loc = bytestream2_get_le32(&dgb);
  339. u0 = (u0 << 4) | (u0 & 0xF);
  340. v0 = (v0 << 4) | (v0 & 0xF);
  341. u1 = (u1 << 4) | (u1 & 0xF);
  342. v1 = (v1 << 4) | (v1 & 0xF);
  343. udif = u1 - u0;
  344. vdif = v1 - v0;
  345. for (int ii = 0; ii < 8; ii += 2) {
  346. for (int jj = 0; jj < 8; jj += 2) {
  347. for (int iii = 0; iii < 2; iii++) {
  348. for (int jjj = 0; jjj < 2; jjj++) {
  349. u[i + ii + iii][j + jj + jjj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
  350. v[i + ii + iii][j + jj + jjj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
  351. }
  352. }
  353. loc >>= 2;
  354. }
  355. }
  356. } else if (escape) {
  357. for (int ii = 0; ii < 8; ii += 4) {
  358. for (int jj = 0; jj < 8; jj += 4) {
  359. u0 = bytestream2_get_byte(&dgb);
  360. v0 = bytestream2_get_byte(&dgb);
  361. u1 = bytestream2_get_byte(&dgb);
  362. v1 = bytestream2_get_byte(&dgb);
  363. loc = bytestream2_get_le32(&dgb);
  364. u0 = (u0 << 4) | (u0 & 0xF);
  365. v0 = (v0 << 4) | (v0 & 0xF);
  366. u1 = (u1 << 4) | (u1 & 0xF);
  367. v1 = (v1 << 4) | (v1 & 0xF);
  368. udif = u1 - u0;
  369. vdif = v1 - v0;
  370. for (int iii = 0; iii < 4; iii++) {
  371. for (int jjj = 0; jjj < 4; jjj++) {
  372. u[i + ii + iii][j + jj + jjj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
  373. v[i + ii + iii][j + jj + jjj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
  374. loc >>= 2;
  375. }
  376. }
  377. }
  378. }
  379. }
  380. is8x8 >>= 1;
  381. }
  382. }
  383. }
  384. for (int i = 0; i < 16; i++) {
  385. for (int j = 0; j < 16; j++) {
  386. dstu[x + i * ulinesize + j] = u[i][j];
  387. dstv[x + i * vlinesize + j] = v[i][j];
  388. }
  389. }
  390. }
  391. dstu += 16 * ulinesize;
  392. dstv += 16 * vlinesize;
  393. }
  394. return 0;
  395. }
  396. static int decode_frame(AVCodecContext *avctx,
  397. void *data, int *got_frame,
  398. AVPacket *avpkt)
  399. {
  400. NotchLCContext *s = avctx->priv_data;
  401. ThreadFrame frame = { .f = data };
  402. GetByteContext *gb = &s->gb;
  403. PutByteContext *pb = &s->pb;
  404. unsigned uncompressed_size;
  405. AVFrame *p = data;
  406. int ret;
  407. if (avpkt->size <= 40)
  408. return AVERROR_INVALIDDATA;
  409. bytestream2_init(gb, avpkt->data, avpkt->size);
  410. if (bytestream2_get_le32(gb) != MKBETAG('N','L','C','1'))
  411. return AVERROR_INVALIDDATA;
  412. uncompressed_size = bytestream2_get_le32(gb);
  413. s->compressed_size = bytestream2_get_le32(gb);
  414. s->format = bytestream2_get_le32(gb);
  415. if (s->format > 2)
  416. return AVERROR_PATCHWELCOME;
  417. if (s->format == 0) {
  418. ret = ff_lzf_uncompress(gb, &s->lzf_buffer, &s->lzf_size);
  419. if (ret < 0)
  420. return ret;
  421. if (uncompressed_size > s->lzf_size)
  422. return AVERROR_INVALIDDATA;
  423. bytestream2_init(gb, s->lzf_buffer, uncompressed_size);
  424. } else if (s->format == 1) {
  425. av_fast_padded_malloc(&s->uncompressed_buffer, &s->uncompressed_size,
  426. uncompressed_size);
  427. if (!s->uncompressed_buffer)
  428. return AVERROR(ENOMEM);
  429. bytestream2_init_writer(pb, s->uncompressed_buffer, s->uncompressed_size);
  430. ret = lz4_decompress(avctx, gb, pb);
  431. if (ret != uncompressed_size)
  432. return AVERROR_INVALIDDATA;
  433. bytestream2_init(gb, s->uncompressed_buffer, uncompressed_size);
  434. }
  435. ret = decode_blocks(avctx, p, &frame, uncompressed_size);
  436. if (ret < 0)
  437. return ret;
  438. p->pict_type = AV_PICTURE_TYPE_I;
  439. p->key_frame = 1;
  440. *got_frame = 1;
  441. return avpkt->size;
  442. }
  443. static av_cold int decode_end(AVCodecContext *avctx)
  444. {
  445. NotchLCContext *s = avctx->priv_data;
  446. av_freep(&s->uncompressed_buffer);
  447. s->uncompressed_size = 0;
  448. av_freep(&s->lzf_buffer);
  449. s->lzf_size = 0;
  450. return 0;
  451. }
  452. AVCodec ff_notchlc_decoder = {
  453. .name = "notchlc",
  454. .long_name = NULL_IF_CONFIG_SMALL("NotchLC"),
  455. .type = AVMEDIA_TYPE_VIDEO,
  456. .id = AV_CODEC_ID_NOTCHLC,
  457. .priv_data_size = sizeof(NotchLCContext),
  458. .init = decode_init,
  459. .close = decode_end,
  460. .decode = decode_frame,
  461. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  462. };