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.

542 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. dsty = (uint16_t *)p->data[0];
  199. dsta = (uint16_t *)p->data[3];
  200. ylinesize = p->linesize[0] / 2;
  201. alinesize = p->linesize[3] / 2;
  202. for (int y = 0; y < avctx->height; y += 4) {
  203. const unsigned row_offset = bytestream2_get_le32(&rgb);
  204. bytestream2_seek(&dgb, s->y_data_offset + row_offset, SEEK_SET);
  205. init_get_bits8(&bit, dgb.buffer, bytestream2_get_bytes_left(&dgb));
  206. for (int x = 0; x < avctx->width; x += 4) {
  207. unsigned item = bytestream2_get_le32(gb);
  208. unsigned y_min = item & 4095;
  209. unsigned y_max = (item >> 12) & 4095;
  210. unsigned y_diff = y_max - y_min;
  211. unsigned control[4];
  212. control[0] = (item >> 24) & 3;
  213. control[1] = (item >> 26) & 3;
  214. control[2] = (item >> 28) & 3;
  215. control[3] = (item >> 30) & 3;
  216. for (int i = 0; i < 4; i++) {
  217. const int nb_bits = control[i] + 1;
  218. const int div = (1 << nb_bits) - 1;
  219. const int add = div - 1;
  220. dsty[x + i * ylinesize + 0] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
  221. dsty[x + i * ylinesize + 1] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
  222. dsty[x + i * ylinesize + 2] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
  223. dsty[x + i * ylinesize + 3] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
  224. }
  225. }
  226. dsty += 4 * ylinesize;
  227. }
  228. rgb = *gb;
  229. dgb = *gb;
  230. bytestream2_seek(gb, s->a_control_word_offset, SEEK_SET);
  231. if (s->uv_count_offset == s->a_control_word_offset) {
  232. for (int y = 0; y < avctx->height; y++) {
  233. for (int x = 0; x < avctx->width; x++)
  234. dsta[x] = 4095;
  235. dsta += alinesize;
  236. }
  237. } else {
  238. for (int y = 0; y < avctx->height; y += 16) {
  239. for (int x = 0; x < avctx->width; x += 16) {
  240. unsigned m = bytestream2_get_le32(gb);
  241. unsigned offset = bytestream2_get_le32(gb);
  242. unsigned alpha0, alpha1;
  243. uint64_t control;
  244. if (offset >= UINT_MAX / 4)
  245. return AVERROR_INVALIDDATA;
  246. offset = offset * 4 + s->uv_data_offset + s->a_data_offset;
  247. if (offset >= s->data_end)
  248. return AVERROR_INVALIDDATA;
  249. bytestream2_seek(&dgb, offset, SEEK_SET);
  250. control = bytestream2_get_le64(&dgb);
  251. alpha0 = control & 0xFF;
  252. alpha1 = (control >> 8) & 0xFF;
  253. control = control >> 16;
  254. for (int by = 0; by < 4; by++) {
  255. for (int bx = 0; bx < 4; bx++) {
  256. switch (m & 3) {
  257. case 0:
  258. for (int i = 0; i < 4; i++) {
  259. for (int j = 0; j < 4; j++) {
  260. dsta[x + (i + by * 4) * alinesize + bx * 4 + j] = 0;
  261. }
  262. }
  263. break;
  264. case 1:
  265. for (int i = 0; i < 4; i++) {
  266. for (int j = 0; j < 4; j++) {
  267. dsta[x + (i + by * 4) * alinesize + bx * 4 + j] = 4095;
  268. }
  269. }
  270. break;
  271. case 2:
  272. for (int i = 0; i < 4; i++) {
  273. for (int j = 0; j < 4; j++) {
  274. dsta[x + (i + by * 4) * alinesize + bx * 4 + j] = (alpha0 + (alpha1 - alpha0) * (control & 7)) << 4;
  275. }
  276. }
  277. break;
  278. default:
  279. return AVERROR_INVALIDDATA;
  280. }
  281. control >>= 3;
  282. m >>= 2;
  283. }
  284. }
  285. }
  286. dsta += 16 * alinesize;
  287. }
  288. }
  289. bytestream2_seek(&rgb, s->uv_offset_data_offset, SEEK_SET);
  290. dstu = (uint16_t *)p->data[1];
  291. dstv = (uint16_t *)p->data[2];
  292. ulinesize = p->linesize[1] / 2;
  293. vlinesize = p->linesize[2] / 2;
  294. for (int y = 0; y < avctx->height; y += 16) {
  295. for (int x = 0; x < avctx->width; x += 16) {
  296. unsigned offset = bytestream2_get_le32(&rgb) * 4;
  297. int u[16][16] = { 0 }, v[16][16] = { 0 };
  298. int u0, v0, u1, v1, udif, vdif;
  299. unsigned escape, is8x8, loc;
  300. bytestream2_seek(&dgb, s->uv_data_offset + offset, SEEK_SET);
  301. is8x8 = bytestream2_get_le16(&dgb);
  302. escape = bytestream2_get_le16(&dgb);
  303. if (escape == 0 && is8x8 == 0) {
  304. u0 = bytestream2_get_byte(&dgb);
  305. v0 = bytestream2_get_byte(&dgb);
  306. u1 = bytestream2_get_byte(&dgb);
  307. v1 = bytestream2_get_byte(&dgb);
  308. loc = bytestream2_get_le32(&dgb);
  309. u0 = (u0 << 4) | (u0 & 0xF);
  310. v0 = (v0 << 4) | (v0 & 0xF);
  311. u1 = (u1 << 4) | (u1 & 0xF);
  312. v1 = (v1 << 4) | (v1 & 0xF);
  313. udif = u1 - u0;
  314. vdif = v1 - v0;
  315. for (int i = 0; i < 16; i += 4) {
  316. for (int j = 0; j < 16; j += 4) {
  317. for (int ii = 0; ii < 4; ii++) {
  318. for (int jj = 0; jj < 4; jj++) {
  319. u[i + ii][j + jj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
  320. v[i + ii][j + jj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
  321. }
  322. }
  323. loc >>= 2;
  324. }
  325. }
  326. } else {
  327. for (int i = 0; i < 16; i += 8) {
  328. for (int j = 0; j < 16; j += 8) {
  329. if (is8x8 & 1) {
  330. u0 = bytestream2_get_byte(&dgb);
  331. v0 = bytestream2_get_byte(&dgb);
  332. u1 = bytestream2_get_byte(&dgb);
  333. v1 = bytestream2_get_byte(&dgb);
  334. loc = bytestream2_get_le32(&dgb);
  335. u0 = (u0 << 4) | (u0 & 0xF);
  336. v0 = (v0 << 4) | (v0 & 0xF);
  337. u1 = (u1 << 4) | (u1 & 0xF);
  338. v1 = (v1 << 4) | (v1 & 0xF);
  339. udif = u1 - u0;
  340. vdif = v1 - v0;
  341. for (int ii = 0; ii < 8; ii += 2) {
  342. for (int jj = 0; jj < 8; jj += 2) {
  343. for (int iii = 0; iii < 2; iii++) {
  344. for (int jjj = 0; jjj < 2; jjj++) {
  345. u[i + ii + iii][j + jj + jjj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
  346. v[i + ii + iii][j + jj + jjj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
  347. }
  348. }
  349. loc >>= 2;
  350. }
  351. }
  352. } else if (escape) {
  353. for (int ii = 0; ii < 8; ii += 4) {
  354. for (int jj = 0; jj < 8; jj += 4) {
  355. u0 = bytestream2_get_byte(&dgb);
  356. v0 = bytestream2_get_byte(&dgb);
  357. u1 = bytestream2_get_byte(&dgb);
  358. v1 = bytestream2_get_byte(&dgb);
  359. loc = bytestream2_get_le32(&dgb);
  360. u0 = (u0 << 4) | (u0 & 0xF);
  361. v0 = (v0 << 4) | (v0 & 0xF);
  362. u1 = (u1 << 4) | (u1 & 0xF);
  363. v1 = (v1 << 4) | (v1 & 0xF);
  364. udif = u1 - u0;
  365. vdif = v1 - v0;
  366. for (int iii = 0; iii < 4; iii++) {
  367. for (int jjj = 0; jjj < 4; jjj++) {
  368. u[i + ii + iii][j + jj + jjj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
  369. v[i + ii + iii][j + jj + jjj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
  370. loc >>= 2;
  371. }
  372. }
  373. }
  374. }
  375. }
  376. is8x8 >>= 1;
  377. }
  378. }
  379. }
  380. for (int i = 0; i < 16; i++) {
  381. for (int j = 0; j < 16; j++) {
  382. dstu[x + i * ulinesize + j] = u[i][j];
  383. dstv[x + i * vlinesize + j] = v[i][j];
  384. }
  385. }
  386. }
  387. dstu += 16 * ulinesize;
  388. dstv += 16 * vlinesize;
  389. }
  390. return 0;
  391. }
  392. static int decode_frame(AVCodecContext *avctx,
  393. void *data, int *got_frame,
  394. AVPacket *avpkt)
  395. {
  396. NotchLCContext *s = avctx->priv_data;
  397. ThreadFrame frame = { .f = data };
  398. GetByteContext *gb = &s->gb;
  399. PutByteContext *pb = &s->pb;
  400. unsigned uncompressed_size;
  401. AVFrame *p = data;
  402. int ret;
  403. if (avpkt->size <= 40)
  404. return AVERROR_INVALIDDATA;
  405. bytestream2_init(gb, avpkt->data, avpkt->size);
  406. if (bytestream2_get_le32(gb) != MKBETAG('N','L','C','1'))
  407. return AVERROR_INVALIDDATA;
  408. uncompressed_size = bytestream2_get_le32(gb);
  409. s->compressed_size = bytestream2_get_le32(gb);
  410. s->format = bytestream2_get_le32(gb);
  411. if (s->format > 2)
  412. return AVERROR_PATCHWELCOME;
  413. if (s->format == 0) {
  414. ret = ff_lzf_uncompress(gb, &s->lzf_buffer, &s->lzf_size);
  415. if (ret < 0)
  416. return ret;
  417. if (uncompressed_size > s->lzf_size)
  418. return AVERROR_INVALIDDATA;
  419. bytestream2_init(gb, s->lzf_buffer, uncompressed_size);
  420. } else if (s->format == 1) {
  421. av_fast_padded_malloc(&s->uncompressed_buffer, &s->uncompressed_size,
  422. uncompressed_size);
  423. if (!s->uncompressed_buffer)
  424. return AVERROR(ENOMEM);
  425. bytestream2_init_writer(pb, s->uncompressed_buffer, s->uncompressed_size);
  426. ret = lz4_decompress(avctx, gb, pb);
  427. if (ret != uncompressed_size)
  428. return AVERROR_INVALIDDATA;
  429. bytestream2_init(gb, s->uncompressed_buffer, uncompressed_size);
  430. }
  431. ret = decode_blocks(avctx, p, &frame, uncompressed_size);
  432. if (ret < 0)
  433. return ret;
  434. p->pict_type = AV_PICTURE_TYPE_I;
  435. p->key_frame = 1;
  436. *got_frame = 1;
  437. return avpkt->size;
  438. }
  439. static av_cold int decode_end(AVCodecContext *avctx)
  440. {
  441. NotchLCContext *s = avctx->priv_data;
  442. av_freep(&s->uncompressed_buffer);
  443. s->uncompressed_size = 0;
  444. av_freep(&s->lzf_buffer);
  445. s->lzf_size = 0;
  446. return 0;
  447. }
  448. AVCodec ff_notchlc_decoder = {
  449. .name = "notchlc",
  450. .long_name = NULL_IF_CONFIG_SMALL("NotchLC"),
  451. .type = AVMEDIA_TYPE_VIDEO,
  452. .id = AV_CODEC_ID_NOTCHLC,
  453. .priv_data_size = sizeof(NotchLCContext),
  454. .init = decode_init,
  455. .close = decode_end,
  456. .decode = decode_frame,
  457. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  458. };