]> git.sesse.net Git - ffmpeg/blob - libavformat/oggparsedaala.c
Merge commit 'd80f0a4ad634b3949b91b85f21fd608c0cddeef7'
[ffmpeg] / libavformat / oggparsedaala.c
1 /*
2  * Ogg Daala parser
3  * Copyright (C) 2015 Rostislav Pehlivanov <atomnuker gmail com>
4  * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara gmail com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include <stdlib.h>
24 #include "libavcodec/bytestream.h"
25 #include "avformat.h"
26 #include "internal.h"
27 #include "oggdec.h"
28
29 struct DaalaPixFmtMap {
30     enum AVPixelFormat ffmpeg_fmt;
31     int depth;
32     int planes;
33     int xdec[4];
34     int ydec[4];
35 };
36
37 /* Currently supported formats only */
38 static const struct DaalaPixFmtMap list_fmts[] = {
39     { AV_PIX_FMT_YUV420P,  8, 3, {0, 1, 1, 0}, {0, 1, 1, 0} },
40     { AV_PIX_FMT_YUV444P,  8, 3, {0, 0, 0, 0}, {0, 0, 0, 0} }
41 };
42
43 typedef struct DaalaInfoHeader {
44     int init_d;
45     int gpshift;
46     int gpmask;
47     int version_maj;
48     int version_min;
49     int version_sub;
50     int frame_duration;
51     int keyframe_granule_shift;
52     struct DaalaPixFmtMap format;
53 } DaalaInfoHeader;
54
55 static inline int daala_match_pix_fmt(struct DaalaPixFmtMap *fmt)
56 {
57     int i, j;
58     for (i = 0; i < FF_ARRAY_ELEMS(list_fmts); i++) {
59         int match = 0;
60         if (fmt->depth != list_fmts[i].depth)
61             continue;
62         if (fmt->planes != list_fmts[i].planes)
63             continue;
64         for (j = 0; j < fmt->planes; j++) {
65             if (fmt->xdec[j] != list_fmts[i].xdec[j])
66                 continue;
67             if (fmt->ydec[j] != list_fmts[i].ydec[j])
68                 continue;
69             match++;
70         }
71         if (match == fmt->planes)
72             return list_fmts[i].ffmpeg_fmt;
73     }
74     return -1;
75 }
76
77 static int daala_header(AVFormatContext *s, int idx)
78 {
79     int i, err;
80     uint8_t *cdp;
81     GetByteContext gb;
82     AVRational timebase;
83     struct ogg *ogg        = s->priv_data;
84     struct ogg_stream *os  = ogg->streams + idx;
85     AVStream *st           = s->streams[idx];
86     int cds                = st->codec->extradata_size + os->psize + 2;
87     DaalaInfoHeader *hdr   = os->private;
88
89     if (!(os->buf[os->pstart] & 0x80))
90         return 0;
91
92     if (!hdr) {
93         hdr = av_mallocz(sizeof(*hdr));
94         if (!hdr)
95             return AVERROR(ENOMEM);
96         os->private = hdr;
97     }
98
99     switch (os->buf[os->pstart]) {
100     case 0x80:
101         bytestream2_init(&gb, os->buf + os->pstart, os->psize);
102         bytestream2_skip(&gb, ff_daala_codec.magicsize);
103
104         hdr->version_maj = bytestream2_get_byte(&gb);
105         hdr->version_min = bytestream2_get_byte(&gb);
106         hdr->version_sub = bytestream2_get_byte(&gb);
107
108         st->codec->width  = bytestream2_get_ne32(&gb);
109         st->codec->height = bytestream2_get_ne32(&gb);
110
111         st->sample_aspect_ratio.num = bytestream2_get_ne32(&gb);
112         st->sample_aspect_ratio.den = bytestream2_get_ne32(&gb);
113
114         timebase.num = bytestream2_get_ne32(&gb);
115         timebase.den = bytestream2_get_ne32(&gb);
116         if (timebase.num < 0 && timebase.den < 0) {
117             av_log(s, AV_LOG_WARNING, "Invalid timebase, assuming 30 FPS\n");
118             timebase.num = 1;
119             timebase.den = 30;
120         }
121         avpriv_set_pts_info(st, 64, timebase.den, timebase.num);
122
123         hdr->frame_duration = bytestream2_get_ne32(&gb);
124         hdr->gpshift = bytestream2_get_byte(&gb);
125         hdr->gpmask  = (1 << hdr->gpshift) - 1;
126
127         hdr->format.depth  = 8 + 2*(bytestream2_get_byte(&gb)-1);
128         hdr->format.planes = bytestream2_get_byte(&gb);
129         for (i = 0; i < hdr->format.planes; i++) {
130             hdr->format.xdec[i] = bytestream2_get_byte(&gb);
131             hdr->format.ydec[i] = bytestream2_get_byte(&gb);
132         }
133
134         if ((st->codec->pix_fmt = daala_match_pix_fmt(&hdr->format)) < 0)
135             av_log(s, AV_LOG_ERROR, "Unsupported pixel format - %i %i\n",
136                    hdr->format.depth, hdr->format.planes);
137
138         st->codec->codec_id   = AV_CODEC_ID_DAALA;
139         st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
140         st->need_parsing      = AVSTREAM_PARSE_HEADERS;
141
142         hdr->init_d = 1;
143         break;
144     case 0x81:
145         if (!hdr->init_d)
146             return AVERROR_INVALIDDATA;
147         ff_vorbis_stream_comment(s, st,
148                                  os->buf + os->pstart + ff_daala_codec.magicsize,
149                                  os->psize - ff_daala_codec.magicsize);
150         break;
151     case 0x82:
152         if (!hdr->init_d)
153             return AVERROR_INVALIDDATA;
154         break;
155     default:
156         av_log(s, AV_LOG_ERROR, "Unknown header type %X\n", os->buf[os->pstart]);
157         return AVERROR_INVALIDDATA;
158         break;
159     }
160
161     if ((err = av_reallocp(&st->codec->extradata,
162                            cds + AV_INPUT_BUFFER_PADDING_SIZE)) < 0) {
163         st->codec->extradata_size = 0;
164         return err;
165     }
166
167     memset(st->codec->extradata + cds, 0, AV_INPUT_BUFFER_PADDING_SIZE);
168     cdp    = st->codec->extradata + st->codec->extradata_size;
169     *cdp++ = os->psize >> 8;
170     *cdp++ = os->psize & 0xff;
171     memcpy(cdp, os->buf + os->pstart, os->psize);
172     st->codec->extradata_size = cds;
173
174     return 1;
175 }
176
177 static uint64_t daala_gptopts(AVFormatContext *ctx, int idx, uint64_t gp,
178                               int64_t *dts)
179 {
180     uint64_t iframe, pframe;
181     struct ogg *ogg       = ctx->priv_data;
182     struct ogg_stream *os = ogg->streams + idx;
183     DaalaInfoHeader *hdr  = os->private;
184
185     if (!hdr)
186         return AV_NOPTS_VALUE;
187
188     iframe = gp >> hdr->gpshift;
189     pframe = gp  & hdr->gpmask;
190
191     if (!pframe)
192         os->pflags |= AV_PKT_FLAG_KEY;
193
194     if (dts)
195         *dts = iframe + pframe;
196
197     return iframe + pframe;
198 }
199
200 static int daala_packet(AVFormatContext *s, int idx)
201 {
202     int seg, duration = 1;
203     struct ogg *ogg = s->priv_data;
204     struct ogg_stream *os = ogg->streams + idx;
205
206     /*
207      * first packet handling: here we parse the duration of each packet in the
208      * first page and compare the total duration to the page granule to find the
209      * encoder delay and set the first timestamp
210      */
211
212     if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) && !(os->flags & OGG_FLAG_EOS)) {
213         for (seg = os->segp; seg < os->nsegs; seg++)
214             if (os->segments[seg] < 255)
215                 duration++;
216
217         os->lastpts = os->lastdts = daala_gptopts(s, idx, os->granule, NULL) - duration;
218         if(s->streams[idx]->start_time == AV_NOPTS_VALUE) {
219             s->streams[idx]->start_time = os->lastpts;
220             if (s->streams[idx]->duration)
221                 s->streams[idx]->duration -= s->streams[idx]->start_time;
222         }
223     }
224
225     /* parse packet duration */
226     if (os->psize > 0)
227         os->pduration = 1;
228
229     return 0;
230 }
231
232 const struct ogg_codec ff_daala_codec = {
233     .name             = "Daala",
234     .magic            = "\200daala",
235     .magicsize        = 6,
236     .header           = daala_header,
237     .packet           = daala_packet,
238     .gptopts          = daala_gptopts,
239     .granule_is_start = 1,
240     .nb_header        = 3,
241 };