3 * Copyright (C) 2015 Rostislav Pehlivanov <atomnuker gmail com>
4 * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara gmail com>
6 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavcodec/bytestream.h"
29 struct DaalaPixFmtMap {
30 enum AVPixelFormat ffmpeg_fmt;
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} }
43 typedef struct DaalaInfoHeader {
52 int keyframe_granule_shift;
53 struct DaalaPixFmtMap format;
56 static inline int daala_match_pix_fmt(struct DaalaPixFmtMap *fmt)
59 for (i = 0; i < FF_ARRAY_ELEMS(list_fmts); i++) {
61 if (fmt->depth != list_fmts[i].depth)
63 if (fmt->planes != list_fmts[i].planes)
65 for (j = 0; j < fmt->planes; j++) {
66 if (fmt->xdec[j] != list_fmts[i].xdec[j])
68 if (fmt->ydec[j] != list_fmts[i].ydec[j])
72 if (match == fmt->planes)
73 return list_fmts[i].ffmpeg_fmt;
78 static int daala_header(AVFormatContext *s, int idx)
84 struct ogg *ogg = s->priv_data;
85 struct ogg_stream *os = ogg->streams + idx;
86 AVStream *st = s->streams[idx];
87 int cds = st->codecpar->extradata_size + os->psize + 2;
88 DaalaInfoHeader *hdr = os->private;
90 if (!(os->buf[os->pstart] & 0x80))
94 hdr = av_mallocz(sizeof(*hdr));
96 return AVERROR(ENOMEM);
100 switch (os->buf[os->pstart]) {
102 bytestream2_init(&gb, os->buf + os->pstart, os->psize);
103 bytestream2_skip(&gb, ff_daala_codec.magicsize);
105 hdr->version_maj = bytestream2_get_byte(&gb);
106 hdr->version_min = bytestream2_get_byte(&gb);
107 hdr->version_sub = bytestream2_get_byte(&gb);
109 st->codecpar->width = bytestream2_get_ne32(&gb);
110 st->codecpar->height = bytestream2_get_ne32(&gb);
112 st->sample_aspect_ratio.num = bytestream2_get_ne32(&gb);
113 st->sample_aspect_ratio.den = bytestream2_get_ne32(&gb);
115 timebase.num = bytestream2_get_ne32(&gb);
116 timebase.den = bytestream2_get_ne32(&gb);
117 if (timebase.num < 0 && timebase.den < 0) {
118 av_log(s, AV_LOG_WARNING, "Invalid timebase, assuming 30 FPS\n");
122 avpriv_set_pts_info(st, 64, timebase.den, timebase.num);
124 hdr->frame_duration = bytestream2_get_ne32(&gb);
125 hdr->gpshift = bytestream2_get_byte(&gb);
126 if (hdr->gpshift >= 32) {
127 av_log(s, AV_LOG_ERROR, "Too large gpshift %d (>= 32).\n",
129 return AVERROR_INVALIDDATA;
131 hdr->gpmask = (1U << hdr->gpshift) - 1;
133 hdr->format.depth = 8 + 2*(bytestream2_get_byte(&gb)-1);
135 hdr->fpr = bytestream2_get_byte(&gb);
137 hdr->format.planes = bytestream2_get_byte(&gb);
138 if (hdr->format.planes > 4) {
139 av_log(s, AV_LOG_ERROR,
140 "Invalid number of planes %d in daala pixel format map.\n",
142 return AVERROR_INVALIDDATA;
144 for (i = 0; i < hdr->format.planes; i++) {
145 hdr->format.xdec[i] = bytestream2_get_byte(&gb);
146 hdr->format.ydec[i] = bytestream2_get_byte(&gb);
149 if ((st->codecpar->format = daala_match_pix_fmt(&hdr->format)) < 0)
150 av_log(s, AV_LOG_ERROR, "Unsupported pixel format - %i %i\n",
151 hdr->format.depth, hdr->format.planes);
153 st->codecpar->codec_id = AV_CODEC_ID_DAALA;
154 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
155 st->need_parsing = AVSTREAM_PARSE_HEADERS;
161 return AVERROR_INVALIDDATA;
162 ff_vorbis_stream_comment(s, st,
163 os->buf + os->pstart + ff_daala_codec.magicsize,
164 os->psize - ff_daala_codec.magicsize);
168 return AVERROR_INVALIDDATA;
171 av_log(s, AV_LOG_ERROR, "Unknown header type %X\n", os->buf[os->pstart]);
172 return AVERROR_INVALIDDATA;
176 if ((err = av_reallocp(&st->codecpar->extradata,
177 cds + AV_INPUT_BUFFER_PADDING_SIZE)) < 0) {
178 st->codecpar->extradata_size = 0;
182 memset(st->codecpar->extradata + cds, 0, AV_INPUT_BUFFER_PADDING_SIZE);
183 cdp = st->codecpar->extradata + st->codecpar->extradata_size;
184 *cdp++ = os->psize >> 8;
185 *cdp++ = os->psize & 0xff;
186 memcpy(cdp, os->buf + os->pstart, os->psize);
187 st->codecpar->extradata_size = cds;
192 static uint64_t daala_gptopts(AVFormatContext *ctx, int idx, uint64_t gp,
195 uint64_t iframe, pframe;
196 struct ogg *ogg = ctx->priv_data;
197 struct ogg_stream *os = ogg->streams + idx;
198 DaalaInfoHeader *hdr = os->private;
201 return AV_NOPTS_VALUE;
203 iframe = gp >> hdr->gpshift;
204 pframe = gp & hdr->gpmask;
207 os->pflags |= AV_PKT_FLAG_KEY;
210 *dts = iframe + pframe;
212 return iframe + pframe;
215 static int daala_packet(AVFormatContext *s, int idx)
217 int seg, duration = 1;
218 struct ogg *ogg = s->priv_data;
219 struct ogg_stream *os = ogg->streams + idx;
222 * first packet handling: here we parse the duration of each packet in the
223 * first page and compare the total duration to the page granule to find the
224 * encoder delay and set the first timestamp
227 if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) && !(os->flags & OGG_FLAG_EOS)) {
228 for (seg = os->segp; seg < os->nsegs; seg++)
229 if (os->segments[seg] < 255)
232 os->lastpts = os->lastdts = daala_gptopts(s, idx, os->granule, NULL) - duration;
233 if(s->streams[idx]->start_time == AV_NOPTS_VALUE) {
234 s->streams[idx]->start_time = os->lastpts;
235 if (s->streams[idx]->duration)
236 s->streams[idx]->duration -= s->streams[idx]->start_time;
240 /* parse packet duration */
247 const struct ogg_codec ff_daala_codec = {
249 .magic = "\200daala",
251 .header = daala_header,
252 .packet = daala_packet,
253 .gptopts = daala_gptopts,
254 .granule_is_start = 1,