/src/ffmpeg/libavcodec/hdr_parser.c
Line | Count | Source |
1 | | /* |
2 | | * Radiance HDR parser |
3 | | * Copyright (c) 2022 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 | | /** |
23 | | * @file |
24 | | * Radiance HDR parser |
25 | | */ |
26 | | |
27 | | #include "libavutil/intreadwrite.h" |
28 | | #include "parser.h" |
29 | | #include "parser_internal.h" |
30 | | |
31 | | typedef struct HDRParseContext { |
32 | | ParseContext pc; |
33 | | } HDRParseContext; |
34 | | |
35 | | static int hdr_parse(AVCodecParserContext *s, AVCodecContext *avctx, |
36 | | const uint8_t **poutbuf, int *poutbuf_size, |
37 | | const uint8_t *buf, int buf_size) |
38 | 280k | { |
39 | 280k | HDRParseContext *ipc = s->priv_data; |
40 | 280k | uint64_t state = ipc->pc.state64; |
41 | 280k | int next = END_NOT_FOUND, i = 0; |
42 | | |
43 | 280k | *poutbuf_size = 0; |
44 | 280k | *poutbuf = NULL; |
45 | | |
46 | 280k | if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
47 | 0 | next = buf_size; |
48 | 280k | } else { |
49 | 43.3M | for (; i < buf_size; i++) { |
50 | 43.1M | state = (state << 8) | buf[i]; |
51 | 43.1M | if (state == AV_RB64("ADIANCE\n") && (i > 10 || ipc->pc.index > 10)) { |
52 | 128k | next = i - 10; |
53 | 128k | break; |
54 | 128k | } |
55 | 43.1M | } |
56 | | |
57 | 280k | ipc->pc.state64 = state; |
58 | 280k | if (ff_combine_frame(&ipc->pc, next, &buf, &buf_size) < 0) { |
59 | 152k | *poutbuf = NULL; |
60 | 152k | *poutbuf_size = 0; |
61 | 152k | return buf_size; |
62 | 152k | } |
63 | 280k | } |
64 | | |
65 | 128k | *poutbuf = buf; |
66 | 128k | *poutbuf_size = buf_size; |
67 | | |
68 | 128k | s->pict_type = AV_PICTURE_TYPE_I; |
69 | 128k | s->key_frame = 1; |
70 | 128k | s->duration = 1; |
71 | | |
72 | 128k | return next; |
73 | 280k | } |
74 | | |
75 | | const FFCodecParser ff_hdr_parser = { |
76 | | PARSER_CODEC_LIST(AV_CODEC_ID_RADIANCE_HDR), |
77 | | .priv_data_size = sizeof(HDRParseContext), |
78 | | .parse = hdr_parse, |
79 | | .close = ff_parse_close, |
80 | | }; |