2 * Copyright (C) 2008 David Conrad
4 * This file is part of Libav.
6 * Libav 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.
11 * Libav 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/intreadwrite.h"
22 #include "libavcodec/dirac.h"
27 static int dirac_header(AVFormatContext *s, int idx)
29 struct ogg *ogg = s->priv_data;
30 struct ogg_stream *os = ogg->streams + idx;
31 AVStream *st = s->streams[idx];
32 AVDiracSeqHeader *dsh;
35 // already parsed the header
36 if (st->codecpar->codec_id == AV_CODEC_ID_DIRAC)
39 ret = av_dirac_parse_sequence_header(&dsh, os->buf + os->pstart + 13, (os->psize - 13) * 8, s);
43 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
44 st->codecpar->codec_id = AV_CODEC_ID_DIRAC;
45 st->codecpar->width = dsh->width;
46 st->codecpar->height = dsh->height;
47 st->codecpar->format = dsh->pix_fmt;
48 st->codecpar->color_range = dsh->color_range;
49 st->codecpar->color_trc = dsh->color_trc;
50 st->codecpar->color_primaries = dsh->color_primaries;
51 st->codecpar->color_space = dsh->colorspace;
52 st->codecpar->profile = dsh->profile;
53 st->codecpar->level = dsh->level;
55 // Dirac in Ogg always stores timestamps as though the video were interlaced
56 avpriv_set_pts_info(st, 64, dsh->framerate.den, 2 * dsh->framerate.num);
63 // various undocumented things: granule is signed (only for Dirac!)
64 static uint64_t dirac_gptopts(AVFormatContext *s, int idx, uint64_t granule,
68 struct ogg *ogg = s->priv_data;
69 struct ogg_stream *os = ogg->streams + idx;
71 unsigned dist = ((gp >> 14) & 0xff00) | (gp & 0xff);
72 int64_t dts = (gp >> 31);
73 int64_t pts = dts + ((gp >> 9) & 0x1fff);
76 os->pflags |= AV_PKT_FLAG_KEY;
84 static int old_dirac_header(AVFormatContext *s, int idx)
86 struct ogg *ogg = s->priv_data;
87 struct ogg_stream *os = ogg->streams + idx;
88 AVStream *st = s->streams[idx];
89 uint8_t *buf = os->buf + os->pstart;
94 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
95 st->codecpar->codec_id = AV_CODEC_ID_DIRAC;
96 avpriv_set_pts_info(st, 64, AV_RB32(buf+12), AV_RB32(buf+8));
100 static uint64_t old_dirac_gptopts(AVFormatContext *s, int idx, uint64_t gp,
103 struct ogg *ogg = s->priv_data;
104 struct ogg_stream *os = ogg->streams + idx;
105 uint64_t iframe = gp >> 30;
106 uint64_t pframe = gp & 0x3fffffff;
109 os->pflags |= AV_PKT_FLAG_KEY;
111 return iframe + pframe;
114 const struct ogg_codec ff_dirac_codec = {
117 .header = dirac_header,
118 .gptopts = dirac_gptopts,
119 .granule_is_start = 1,
123 const struct ogg_codec ff_old_dirac_codec = {
126 .header = old_dirac_header,
127 .gptopts = old_dirac_gptopts,
128 .granule_is_start = 1,