FFmpeg
oggparsedirac.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 David Conrad
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 "libavutil/imgutils.h"
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/mem.h"
24 #include "libavcodec/dirac.h"
25 #include "avformat.h"
26 #include "internal.h"
27 #include "oggdec.h"
28 
29 static int dirac_header(AVFormatContext *s, int idx)
30 {
31  struct ogg *ogg = s->priv_data;
32  struct ogg_stream *os = ogg->streams + idx;
33  AVStream *st = s->streams[idx];
34  AVDiracSeqHeader *dsh;
35  int ret;
36 
37  // already parsed the header
39  return 0;
40 
41  ret = av_dirac_parse_sequence_header(&dsh, os->buf + os->pstart + 13, (os->psize - 13), s);
42  if (ret < 0)
43  return ret;
44 
47  st->codecpar->width = dsh->width;
48  st->codecpar->height = dsh->height;
49  st->codecpar->format = dsh->pix_fmt;
50  st->codecpar->color_range = dsh->color_range;
51  st->codecpar->color_trc = dsh->color_trc;
53  st->codecpar->color_space = dsh->colorspace;
54  st->codecpar->profile = dsh->profile;
55  st->codecpar->level = dsh->level;
58 
59  // Dirac in Ogg always stores timestamps as though the video were interlaced
60  avpriv_set_pts_info(st, 64, dsh->framerate.den, 2 * dsh->framerate.num);
61 
62  av_freep(&dsh);
63  return 1;
64 }
65 
66 // various undocumented things: granule is signed (only for Dirac!)
67 static uint64_t dirac_gptopts(AVFormatContext *s, int idx, uint64_t granule,
68  int64_t *dts_out)
69 {
70  int64_t gp = granule;
71  struct ogg *ogg = s->priv_data;
72  struct ogg_stream *os = ogg->streams + idx;
73 
74  unsigned dist = ((gp >> 14) & 0xff00) | (gp & 0xff);
75  int64_t dts = (gp >> 31);
76  int64_t pts = dts + ((gp >> 9) & 0x1fff);
77 
78  if (!dist)
79  os->pflags |= AV_PKT_FLAG_KEY;
80 
81  if (dts_out)
82  *dts_out = dts;
83 
84  return pts;
85 }
86 
87 static int old_dirac_header(AVFormatContext *s, int idx)
88 {
89  struct ogg *ogg = s->priv_data;
90  struct ogg_stream *os = ogg->streams + idx;
91  AVStream *st = s->streams[idx];
92  uint8_t *buf = os->buf + os->pstart;
93 
94  if (buf[0] != 'K')
95  return 0;
96 
99  avpriv_set_pts_info(st, 64, AV_RB32(buf+12), AV_RB32(buf+8));
100  return 1;
101 }
102 
103 static uint64_t old_dirac_gptopts(AVFormatContext *s, int idx, uint64_t gp,
104  int64_t *dts)
105 {
106  struct ogg *ogg = s->priv_data;
107  struct ogg_stream *os = ogg->streams + idx;
108  uint64_t iframe = gp >> 30;
109  uint64_t pframe = gp & 0x3fffffff;
110 
111  if (!pframe)
112  os->pflags |= AV_PKT_FLAG_KEY;
113 
114  return iframe + pframe;
115 }
116 
117 const struct ogg_codec ff_dirac_codec = {
118  .magic = "BBCD\0",
119  .magicsize = 5,
120  .header = dirac_header,
121  .gptopts = dirac_gptopts,
122  .granule_is_start = 1,
123  .nb_header = 1,
124 };
125 
127  .magic = "KW-DIRAC",
128  .magicsize = 8,
129  .header = old_dirac_header,
130  .gptopts = old_dirac_gptopts,
131  .granule_is_start = 1,
132  .nb_header = 1,
133 };
av_dirac_parse_sequence_header
int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh, const uint8_t *buf, size_t buf_size, void *log_ctx)
Parse a Dirac sequence header.
Definition: dirac.c:404
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AVCodecParameters::color_space
enum AVColorSpace color_space
Definition: codec_par.h:169
old_dirac_gptopts
static uint64_t old_dirac_gptopts(AVFormatContext *s, int idx, uint64_t gp, int64_t *dts)
Definition: oggparsedirac.c:103
AV_CODEC_ID_DIRAC
@ AV_CODEC_ID_DIRAC
Definition: codec_id.h:168
int64_t
long long int64_t
Definition: coverity.c:34
AVDiracSeqHeader::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: dirac.h:113
ogg_stream::granule
uint64_t granule
Definition: oggdec.h:69
dirac_gptopts
static uint64_t dirac_gptopts(AVFormatContext *s, int idx, uint64_t granule, int64_t *dts_out)
Definition: oggparsedirac.c:67
ogg_stream::buf
uint8_t * buf
Definition: oggdec.h:61
AVDiracSeqHeader::color_range
enum AVColorRange color_range
Definition: dirac.h:111
ogg
Definition: oggdec.h:101
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:594
AVDiracSeqHeader::sample_aspect_ratio
AVRational sample_aspect_ratio
Definition: dirac.h:108
AVCodecParameters::color_primaries
enum AVColorPrimaries color_primaries
Definition: codec_par.h:167
AVDiracSeqHeader::level
int level
Definition: dirac.h:105
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:867
pts
static int64_t pts
Definition: transcode_aac.c:644
AVRational::num
int num
Numerator.
Definition: rational.h:59
dirac.h
AVCodecParameters::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: codec_par.h:168
ogg_stream::pstart
unsigned int pstart
Definition: oggdec.h:64
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
AVDiracSeqHeader::profile
int profile
Definition: dirac.h:104
old_dirac_header
static int old_dirac_header(AVFormatContext *s, int idx)
Definition: oggparsedirac.c:87
AVDiracSeqHeader::framerate
AVRational framerate
Definition: dirac.h:107
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
AVDiracSeqHeader::colorspace
enum AVColorSpace colorspace
Definition: dirac.h:114
ff_dirac_codec
const struct ogg_codec ff_dirac_codec
Definition: oggparsedirac.c:117
ogg::streams
struct ogg_stream * streams
Definition: oggdec.h:102
AVCodecParameters::level
int level
Definition: codec_par.h:129
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
AVCodecParameters::profile
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition: codec_par.h:128
AVStream::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:826
ogg_stream::pflags
unsigned int pflags
Definition: oggdec.h:66
ogg_stream
Definition: oggdec.h:60
AVCodecParameters::height
int height
Definition: codec_par.h:135
AVCodecParameters::color_range
enum AVColorRange color_range
Video only.
Definition: codec_par.h:166
AVDiracSeqHeader
Definition: dirac.h:85
dirac_header
static int dirac_header(AVFormatContext *s, int idx)
Definition: oggparsedirac.c:29
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
avformat.h
AVDiracSeqHeader::pix_fmt
enum AVPixelFormat pix_fmt
Definition: dirac.h:110
ff_old_dirac_codec
const struct ogg_codec ff_old_dirac_codec
Definition: oggparsedirac.c:126
oggdec.h
AVRational::den
int den
Denominator.
Definition: rational.h:60
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
AVCodecParameters::format
int format
Definition: codec_par.h:92
AVDiracSeqHeader::width
unsigned width
Definition: dirac.h:86
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
ogg_stream::psize
unsigned int psize
Definition: oggdec.h:65
ogg_codec
Copyright (C) 2005 Michael Ahlberg, Måns Rullgård.
Definition: oggdec.h:30
imgutils.h
AVDiracSeqHeader::height
unsigned height
Definition: dirac.h:87
ogg_codec::magic
const int8_t * magic
Definition: oggdec.h:31
av_image_check_sar
int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
Check if the given sample aspect ratio of an image is valid.
Definition: imgutils.c:323
AVDiracSeqHeader::color_primaries
enum AVColorPrimaries color_primaries
Definition: dirac.h:112