]> git.sesse.net Git - ffmpeg/blob - libavcodec/notchlc.c
x86/cfhddsp: zero extend int arguments
[ffmpeg] / libavcodec / notchlc.c
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
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #define BITSTREAM_READER_LE
27 #include "libavutil/intreadwrite.h"
28 #include "avcodec.h"
29 #include "bytestream.h"
30 #include "get_bits.h"
31 #include "internal.h"
32 #include "lzf.h"
33 #include "thread.h"
34
35 typedef struct NotchLCContext {
36     unsigned compressed_size;
37     unsigned format;
38
39     uint8_t *uncompressed_buffer;
40     unsigned uncompressed_size;
41
42     uint8_t *lzf_buffer;
43     int64_t lzf_size;
44
45     unsigned texture_size_x;
46     unsigned texture_size_y;
47     unsigned y_data_row_offsets;
48     unsigned uv_offset_data_offset;
49     unsigned y_control_data_offset;
50     unsigned a_control_word_offset;
51     unsigned y_data_offset;
52     unsigned uv_data_offset;
53     unsigned y_data_size;
54     unsigned a_data_offset;
55     unsigned uv_count_offset;
56     unsigned a_count_size;
57     unsigned data_end;
58
59     GetByteContext gb;
60     PutByteContext pb;
61 } NotchLCContext;
62
63 static av_cold int decode_init(AVCodecContext *avctx)
64 {
65     avctx->pix_fmt = AV_PIX_FMT_YUVA444P12;
66     avctx->color_range = AVCOL_RANGE_JPEG;
67     avctx->colorspace = AVCOL_SPC_RGB;
68     avctx->color_primaries = AVCOL_PRI_BT709;
69     avctx->color_trc = AVCOL_TRC_IEC61966_2_1;
70
71     return 0;
72 }
73
74 #define HISTORY_SIZE (64 * 1024)
75
76 static int lz4_decompress(AVCodecContext *avctx,
77                           GetByteContext *gb,
78                           PutByteContext *pb)
79 {
80     unsigned reference_pos, match_length, delta, pos = 0;
81     uint8_t history[64 * 1024];
82
83     while (bytestream2_get_bytes_left(gb) > 0) {
84         uint8_t token = bytestream2_get_byte(gb);
85         unsigned num_literals = token >> 4;
86
87         if (num_literals == 15) {
88             unsigned char current;
89             do {
90                 current = bytestream2_get_byte(gb);
91                 num_literals += current;
92             } while (current == 255);
93         }
94
95         if (pos + num_literals < HISTORY_SIZE) {
96             bytestream2_get_buffer(gb, history + pos, num_literals);
97             pos += num_literals;
98         } else {
99             while (num_literals-- > 0) {
100                 history[pos++] = bytestream2_get_byte(gb);
101                 if (pos == HISTORY_SIZE) {
102                     bytestream2_put_buffer(pb, history, HISTORY_SIZE);
103                     pos = 0;
104                 }
105             }
106         }
107
108         if (bytestream2_get_bytes_left(gb) <= 0)
109             break;
110
111         delta = bytestream2_get_byte(gb);
112         delta |= (unsigned)bytestream2_get_byte(gb) << 8;
113         if (delta == 0)
114             return 0;
115         match_length = 4 + (token & 0x0F);
116         if (match_length == 4 + 0x0F) {
117             uint8_t current;
118
119             do {
120                 current = bytestream2_get_byte(gb);
121                 match_length += current;
122             } while (current == 255);
123         }
124         reference_pos = (pos >= delta) ? (pos - delta) : (HISTORY_SIZE + pos - delta);
125         if (pos + match_length < HISTORY_SIZE && reference_pos + match_length < HISTORY_SIZE) {
126             if (pos >= reference_pos + match_length || reference_pos >= pos + match_length) {
127                 memcpy(history + pos, history + reference_pos, match_length);
128                 pos += match_length;
129             } else {
130                 while (match_length-- > 0)
131                     history[pos++] = history[reference_pos++];
132             }
133         } else {
134             while (match_length-- > 0) {
135                 history[pos++] = history[reference_pos++];
136                 if (pos == HISTORY_SIZE) {
137                     bytestream2_put_buffer(pb, history, HISTORY_SIZE);
138                     pos = 0;
139                 }
140                 reference_pos %= HISTORY_SIZE;
141             }
142         }
143     }
144
145     bytestream2_put_buffer(pb, history, pos);
146
147     return bytestream2_tell_p(pb);
148 }
149
150 static int decode_blocks(AVCodecContext *avctx, AVFrame *p, ThreadFrame *frame,
151                          unsigned uncompressed_size)
152 {
153     NotchLCContext *s = avctx->priv_data;
154     GetByteContext rgb, dgb, *gb = &s->gb;
155     GetBitContext bit;
156     int ylinesize, ulinesize, vlinesize, alinesize;
157     uint16_t *dsty, *dstu, *dstv, *dsta;
158     int ret;
159
160     s->texture_size_x = bytestream2_get_le32(gb);
161     s->texture_size_y = bytestream2_get_le32(gb);
162
163     ret = ff_set_dimensions(avctx, s->texture_size_x, s->texture_size_y);
164     if (ret < 0)
165         return ret;
166
167     s->uv_offset_data_offset = bytestream2_get_le32(gb);
168     if (s->uv_offset_data_offset >= UINT_MAX / 4)
169         return AVERROR_INVALIDDATA;
170     s->uv_offset_data_offset *= 4;
171     if (s->uv_offset_data_offset >= uncompressed_size)
172         return AVERROR_INVALIDDATA;
173
174     s->y_control_data_offset = bytestream2_get_le32(gb);
175     if (s->y_control_data_offset >= UINT_MAX / 4)
176         return AVERROR_INVALIDDATA;
177     s->y_control_data_offset *= 4;
178     if (s->y_control_data_offset >= uncompressed_size)
179         return AVERROR_INVALIDDATA;
180
181     s->a_control_word_offset = bytestream2_get_le32(gb);
182     if (s->a_control_word_offset >= UINT_MAX / 4)
183         return AVERROR_INVALIDDATA;
184     s->a_control_word_offset *= 4;
185     if (s->a_control_word_offset >= uncompressed_size)
186         return AVERROR_INVALIDDATA;
187
188     s->uv_data_offset = bytestream2_get_le32(gb);
189     if (s->uv_data_offset >= UINT_MAX / 4)
190         return AVERROR_INVALIDDATA;
191     s->uv_data_offset *= 4;
192     if (s->uv_data_offset >= uncompressed_size)
193         return AVERROR_INVALIDDATA;
194
195     s->y_data_size = bytestream2_get_le32(gb);
196     if (s->y_data_size >= UINT_MAX / 4)
197         return AVERROR_INVALIDDATA;
198
199     s->a_data_offset = bytestream2_get_le32(gb);
200     if (s->a_data_offset >= UINT_MAX / 4)
201         return AVERROR_INVALIDDATA;
202     s->a_data_offset *= 4;
203     if (s->a_data_offset >= uncompressed_size)
204         return AVERROR_INVALIDDATA;
205
206     s->a_count_size = bytestream2_get_le32(gb);
207     if (s->a_count_size >= UINT_MAX / 4)
208         return AVERROR_INVALIDDATA;
209     s->a_count_size *= 4;
210     if (s->a_count_size >= uncompressed_size)
211         return AVERROR_INVALIDDATA;
212
213     s->data_end = bytestream2_get_le32(gb);
214     if (s->data_end > uncompressed_size)
215         return AVERROR_INVALIDDATA;
216
217     s->y_data_row_offsets = bytestream2_tell(gb);
218     if (s->data_end <= s->y_data_size)
219         return AVERROR_INVALIDDATA;
220     s->y_data_offset = s->data_end - s->y_data_size;
221     if (s->y_data_offset <= s->a_data_offset)
222         return AVERROR_INVALIDDATA;
223     s->uv_count_offset = s->y_data_offset - s->a_data_offset;
224
225     if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0)
226         return ret;
227
228     rgb = *gb;
229     dgb = *gb;
230     bytestream2_seek(&rgb, s->y_data_row_offsets, SEEK_SET);
231     bytestream2_seek(gb, s->y_control_data_offset, SEEK_SET);
232
233     dsty = (uint16_t *)p->data[0];
234     dsta = (uint16_t *)p->data[3];
235     ylinesize = p->linesize[0] / 2;
236     alinesize = p->linesize[3] / 2;
237
238     for (int y = 0; y < avctx->height; y += 4) {
239         const unsigned row_offset = bytestream2_get_le32(&rgb);
240
241         bytestream2_seek(&dgb, s->y_data_offset + row_offset, SEEK_SET);
242
243         init_get_bits8(&bit, dgb.buffer, bytestream2_get_bytes_left(&dgb));
244         for (int x = 0; x < avctx->width; x += 4) {
245             unsigned item = bytestream2_get_le32(gb);
246             unsigned y_min = item & 4095;
247             unsigned y_max = (item >> 12) & 4095;
248             unsigned y_diff = y_max - y_min;
249             unsigned control[4];
250
251             control[0] = (item >> 24) & 3;
252             control[1] = (item >> 26) & 3;
253             control[2] = (item >> 28) & 3;
254             control[3] = (item >> 30) & 3;
255
256             for (int i = 0; i < 4; i++) {
257                 const int nb_bits = control[i] + 1;
258                 const int div = (1 << nb_bits) - 1;
259                 const int add = div - 1;
260
261                 dsty[x + i * ylinesize + 0] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
262                 dsty[x + i * ylinesize + 1] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
263                 dsty[x + i * ylinesize + 2] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
264                 dsty[x + i * ylinesize + 3] = av_clip_uintp2(y_min + ((y_diff * get_bits(&bit, nb_bits) + add) / div), 12);
265             }
266         }
267
268         dsty += 4 * ylinesize;
269     }
270
271     rgb = *gb;
272     dgb = *gb;
273     bytestream2_seek(gb, s->a_control_word_offset, SEEK_SET);
274     if (s->uv_count_offset == s->a_control_word_offset) {
275         for (int y = 0; y < avctx->height; y++) {
276             for (int x = 0; x < avctx->width; x++)
277                 dsta[x] = 4095;
278             dsta += alinesize;
279         }
280     } else {
281         for (int y = 0; y < avctx->height; y += 16) {
282             for (int x = 0; x < avctx->width; x += 16) {
283                 unsigned m = bytestream2_get_le32(gb);
284                 unsigned offset = bytestream2_get_le32(gb);
285                 unsigned alpha0, alpha1;
286                 uint64_t control;
287
288                 if (offset >= UINT_MAX / 4)
289                     return AVERROR_INVALIDDATA;
290                 offset = offset * 4 + s->uv_data_offset + s->a_data_offset;
291                 if (offset >= s->data_end)
292                     return AVERROR_INVALIDDATA;
293
294                 bytestream2_seek(&dgb, offset, SEEK_SET);
295                 control = bytestream2_get_le64(&dgb);
296                 alpha0 = control & 0xFF;
297                 alpha1 = (control >> 8) & 0xFF;
298                 control = control >> 16;
299
300                 for (int by = 0; by < 4; by++) {
301                     for (int bx = 0; bx < 4; bx++) {
302                         switch (m & 3) {
303                         case 0:
304                             for (int i = 0; i < 4; i++) {
305                                 for (int j = 0; j < 4; j++) {
306                                     dsta[x + (i + by * 4) * alinesize + bx * 4 + j] = 0;
307                                 }
308                             }
309                             break;
310                         case 1:
311                             for (int i = 0; i < 4; i++) {
312                                 for (int j = 0; j < 4; j++) {
313                                     dsta[x + (i + by * 4) * alinesize + bx * 4 + j] = 4095;
314                                 }
315                             }
316                             break;
317                         case 2:
318                             for (int i = 0; i < 4; i++) {
319                                 for (int j = 0; j < 4; j++) {
320                                     dsta[x + (i + by * 4) * alinesize + bx * 4 + j] = (alpha0 + (alpha1 - alpha0) * (control & 7)) << 4;
321                                 }
322                             }
323                             break;
324                         default:
325                             return AVERROR_INVALIDDATA;
326                         }
327
328                         control >>= 3;
329                         m >>= 2;
330                     }
331                 }
332             }
333
334             dsta += 16 * alinesize;
335         }
336     }
337
338     bytestream2_seek(&rgb, s->uv_offset_data_offset, SEEK_SET);
339
340     dstu = (uint16_t *)p->data[1];
341     dstv = (uint16_t *)p->data[2];
342     ulinesize = p->linesize[1] / 2;
343     vlinesize = p->linesize[2] / 2;
344
345     for (int y = 0; y < avctx->height; y += 16) {
346         for (int x = 0; x < avctx->width; x += 16) {
347             unsigned offset = bytestream2_get_le32(&rgb) * 4;
348             int u[16][16] = { 0 }, v[16][16] = { 0 };
349             int u0, v0, u1, v1, udif, vdif;
350             unsigned escape, is8x8, loc;
351
352             bytestream2_seek(&dgb, s->uv_data_offset + offset, SEEK_SET);
353
354             is8x8 = bytestream2_get_le16(&dgb);
355             escape = bytestream2_get_le16(&dgb);
356
357             if (escape == 0 && is8x8 == 0) {
358                 u0 = bytestream2_get_byte(&dgb);
359                 v0 = bytestream2_get_byte(&dgb);
360                 u1 = bytestream2_get_byte(&dgb);
361                 v1 = bytestream2_get_byte(&dgb);
362                 loc = bytestream2_get_le32(&dgb);
363                 u0 = (u0 << 4) | (u0 & 0xF);
364                 v0 = (v0 << 4) | (v0 & 0xF);
365                 u1 = (u1 << 4) | (u1 & 0xF);
366                 v1 = (v1 << 4) | (v1 & 0xF);
367                 udif = u1 - u0;
368                 vdif = v1 - v0;
369
370                 for (int i = 0; i < 16; i += 4) {
371                     for (int j = 0; j < 16; j += 4) {
372                         for (int ii = 0; ii < 4; ii++) {
373                             for (int jj = 0; jj < 4; jj++) {
374                                 u[i + ii][j + jj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
375                                 v[i + ii][j + jj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
376                             }
377                         }
378
379                         loc >>= 2;
380                     }
381                 }
382             } else {
383                 for (int i = 0; i < 16; i += 8) {
384                     for (int j = 0; j < 16; j += 8) {
385                         if (is8x8 & 1) {
386                             u0 = bytestream2_get_byte(&dgb);
387                             v0 = bytestream2_get_byte(&dgb);
388                             u1 = bytestream2_get_byte(&dgb);
389                             v1 = bytestream2_get_byte(&dgb);
390                             loc = bytestream2_get_le32(&dgb);
391                             u0 = (u0 << 4) | (u0 & 0xF);
392                             v0 = (v0 << 4) | (v0 & 0xF);
393                             u1 = (u1 << 4) | (u1 & 0xF);
394                             v1 = (v1 << 4) | (v1 & 0xF);
395                             udif = u1 - u0;
396                             vdif = v1 - v0;
397
398                             for (int ii = 0; ii < 8; ii += 2) {
399                                 for (int jj = 0; jj < 8; jj += 2) {
400                                     for (int iii = 0; iii < 2; iii++) {
401                                         for (int jjj = 0; jjj < 2; jjj++) {
402                                             u[i + ii + iii][j + jj + jjj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
403                                             v[i + ii + iii][j + jj + jjj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
404                                         }
405                                     }
406
407                                     loc >>= 2;
408                                 }
409                             }
410                         } else if (escape) {
411                             for (int ii = 0; ii < 8; ii += 4) {
412                                 for (int jj = 0; jj < 8; jj += 4) {
413                                     u0 = bytestream2_get_byte(&dgb);
414                                     v0 = bytestream2_get_byte(&dgb);
415                                     u1 = bytestream2_get_byte(&dgb);
416                                     v1 = bytestream2_get_byte(&dgb);
417                                     loc = bytestream2_get_le32(&dgb);
418                                     u0 = (u0 << 4) | (u0 & 0xF);
419                                     v0 = (v0 << 4) | (v0 & 0xF);
420                                     u1 = (u1 << 4) | (u1 & 0xF);
421                                     v1 = (v1 << 4) | (v1 & 0xF);
422                                     udif = u1 - u0;
423                                     vdif = v1 - v0;
424
425                                     for (int iii = 0; iii < 4; iii++) {
426                                         for (int jjj = 0; jjj < 4; jjj++) {
427                                             u[i + ii + iii][j + jj + jjj] = u0 + ((udif * (int)(loc & 3) + 2) / 3);
428                                             v[i + ii + iii][j + jj + jjj] = v0 + ((vdif * (int)(loc & 3) + 2) / 3);
429
430                                             loc >>= 2;
431                                         }
432                                     }
433                                 }
434                             }
435                         }
436
437                         is8x8 >>= 1;
438                     }
439                 }
440             }
441
442             for (int i = 0; i < 16; i++) {
443                 for (int j = 0; j < 16; j++) {
444                     dstu[x + i * ulinesize + j] = u[i][j];
445                     dstv[x + i * vlinesize + j] = v[i][j];
446                 }
447             }
448         }
449
450         dstu += 16 * ulinesize;
451         dstv += 16 * vlinesize;
452     }
453
454     return 0;
455 }
456
457 static int decode_frame(AVCodecContext *avctx,
458                         void *data, int *got_frame,
459                         AVPacket *avpkt)
460 {
461     NotchLCContext *s = avctx->priv_data;
462     ThreadFrame frame = { .f = data };
463     GetByteContext *gb = &s->gb;
464     PutByteContext *pb = &s->pb;
465     unsigned uncompressed_size;
466     AVFrame *p = data;
467     int ret;
468
469     if (avpkt->size <= 40)
470         return AVERROR_INVALIDDATA;
471
472     bytestream2_init(gb, avpkt->data, avpkt->size);
473
474     if (bytestream2_get_le32(gb) != MKBETAG('N','L','C','1'))
475         return AVERROR_INVALIDDATA;
476
477     uncompressed_size = bytestream2_get_le32(gb);
478     s->compressed_size = bytestream2_get_le32(gb);
479     s->format = bytestream2_get_le32(gb);
480
481     if (s->format > 2)
482         return AVERROR_PATCHWELCOME;
483
484     if (s->format == 0) {
485         ret = ff_lzf_uncompress(gb, &s->lzf_buffer, &s->lzf_size);
486         if (ret < 0)
487             return ret;
488
489         if (uncompressed_size > s->lzf_size)
490             return AVERROR_INVALIDDATA;
491
492         bytestream2_init(gb, s->lzf_buffer, uncompressed_size);
493     } else if (s->format == 1) {
494         av_fast_padded_malloc(&s->uncompressed_buffer, &s->uncompressed_size,
495                               uncompressed_size);
496         if (!s->uncompressed_buffer)
497             return AVERROR(ENOMEM);
498
499         bytestream2_init_writer(pb, s->uncompressed_buffer, s->uncompressed_size);
500
501         ret = lz4_decompress(avctx, gb, pb);
502         if (ret != uncompressed_size)
503             return AVERROR_INVALIDDATA;
504
505         bytestream2_init(gb, s->uncompressed_buffer, uncompressed_size);
506     }
507
508     ret = decode_blocks(avctx, p, &frame, uncompressed_size);
509     if (ret < 0)
510         return ret;
511
512     p->pict_type = AV_PICTURE_TYPE_I;
513     p->key_frame = 1;
514
515     *got_frame = 1;
516
517     return avpkt->size;
518 }
519
520 static av_cold int decode_end(AVCodecContext *avctx)
521 {
522     NotchLCContext *s = avctx->priv_data;
523
524     av_freep(&s->uncompressed_buffer);
525     s->uncompressed_size = 0;
526     av_freep(&s->lzf_buffer);
527     s->lzf_size = 0;
528
529     return 0;
530 }
531
532 AVCodec ff_notchlc_decoder = {
533     .name             = "notchlc",
534     .long_name        = NULL_IF_CONFIG_SMALL("NotchLC"),
535     .type             = AVMEDIA_TYPE_VIDEO,
536     .id               = AV_CODEC_ID_NOTCHLC,
537     .priv_data_size   = sizeof(NotchLCContext),
538     .init             = decode_init,
539     .close            = decode_end,
540     .decode           = decode_frame,
541     .capabilities     = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
542 };