]> git.sesse.net Git - ffmpeg/blob - libavcodec/av1_parse.c
50dd940f03619dd0bcff53eaf654daf4d126177c
[ffmpeg] / libavcodec / av1_parse.c
1 /*
2  * AV1 common parsing code
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "config.h"
22
23 #include "libavutil/mem.h"
24
25 #include "av1_parse.h"
26 #include "bytestream.h"
27
28 int ff_av1_extract_obu(AV1OBU *obu, const uint8_t *buf, int length, void *logctx)
29 {
30     int64_t obu_size;
31     int start_pos, type, temporal_id, spatial_id;
32     int len, ret;
33
34     len = parse_obu_header(buf, length, &obu_size, &start_pos,
35                            &type, &temporal_id, &spatial_id);
36     if (len < 0)
37         return len;
38
39     if (obu_size > INT_MAX / 8 || obu_size < 0)
40         return AVERROR(ERANGE);
41
42     obu->type        = type;
43     obu->temporal_id = temporal_id;
44     obu->spatial_id  = spatial_id;
45
46     obu->data     = buf + start_pos;
47     obu->size     = obu_size;
48     obu->raw_data = buf;
49     obu->raw_size = len;
50
51     ret = init_get_bits(&obu->gb, obu->data, obu->size * 8);
52     if (ret < 0)
53         return ret;
54
55     av_log(logctx, AV_LOG_DEBUG,
56            "obu_type: %d, temporal_id: %d, spatial_id: %d, payload size: %d\n",
57            obu->type, obu->temporal_id, obu->spatial_id, obu->size);
58
59     return len;
60 }
61
62 int ff_av1_packet_split(AV1Packet *pkt, const uint8_t *buf, int length, void *logctx)
63 {
64     GetByteContext bc;
65     int consumed;
66
67     bytestream2_init(&bc, buf, length);
68     pkt->nb_obus = 0;
69
70     while (bytestream2_get_bytes_left(&bc) > 0) {
71         AV1OBU *obu;
72
73         if (pkt->obus_allocated < pkt->nb_obus + 1) {
74             int new_size = pkt->obus_allocated + 1;
75             AV1OBU *tmp = av_realloc_array(pkt->obus, new_size, sizeof(*tmp));
76             if (!tmp)
77                 return AVERROR(ENOMEM);
78
79             pkt->obus = tmp;
80             memset(pkt->obus + pkt->obus_allocated, 0,
81                    (new_size - pkt->obus_allocated) * sizeof(*tmp));
82             pkt->obus_allocated = new_size;
83         }
84         obu = &pkt->obus[pkt->nb_obus];
85
86         consumed = ff_av1_extract_obu(obu, bc.buffer, bytestream2_get_bytes_left(&bc), logctx);
87         if (consumed < 0)
88             return consumed;
89
90         pkt->nb_obus++;
91
92         bytestream2_skip(&bc, consumed);
93     }
94
95     return 0;
96 }
97
98 void ff_av1_packet_uninit(AV1Packet *pkt)
99 {
100     av_freep(&pkt->obus);
101     pkt->obus_allocated = 0;
102 }