Libav
oggparsedirac.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 David Conrad
3  *
4  * This file is part of Libav.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include "libavutil/intreadwrite.h"
22 #include "libavcodec/dirac.h"
23 #include "avformat.h"
24 #include "internal.h"
25 #include "oggdec.h"
26 
27 static int dirac_header(AVFormatContext *s, int idx)
28 {
29  struct ogg *ogg = s->priv_data;
30  struct ogg_stream *os = ogg->streams + idx;
31  AVStream *st = s->streams[idx];
32  AVDiracSeqHeader *dsh;
33  int ret;
34 
35  // already parsed the header
37  return 0;
38 
39  ret = av_dirac_parse_sequence_header(&dsh, os->buf + os->pstart + 13, (os->psize - 13) * 8, s);
40  if (ret < 0)
41  return ret;
42 
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;
51  st->codecpar->color_space = dsh->colorspace;
52  st->codecpar->profile = dsh->profile;
53  st->codecpar->level = dsh->level;
54 
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);
57 
58  av_freep(&dsh);
59 
60  return 1;
61 }
62 
63 // various undocumented things: granule is signed (only for Dirac!)
64 static uint64_t dirac_gptopts(AVFormatContext *s, int idx, uint64_t granule,
65  int64_t *dts_out)
66 {
67  int64_t gp = granule;
68  struct ogg *ogg = s->priv_data;
69  struct ogg_stream *os = ogg->streams + idx;
70 
71  unsigned dist = ((gp >> 14) & 0xff00) | (gp & 0xff);
72  int64_t dts = (gp >> 31);
73  int64_t pts = dts + ((gp >> 9) & 0x1fff);
74 
75  if (!dist)
76  os->pflags |= AV_PKT_FLAG_KEY;
77 
78  if (dts_out)
79  *dts_out = dts;
80 
81  return pts;
82 }
83 
84 static int old_dirac_header(AVFormatContext *s, int idx)
85 {
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;
90 
91  if (buf[0] != 'K')
92  return 0;
93 
96  avpriv_set_pts_info(st, 64, AV_RB32(buf+12), AV_RB32(buf+8));
97  return 1;
98 }
99 
100 static uint64_t old_dirac_gptopts(AVFormatContext *s, int idx, uint64_t gp,
101  int64_t *dts)
102 {
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;
107 
108  if (!pframe)
109  os->pflags |= AV_PKT_FLAG_KEY;
110 
111  return iframe + pframe;
112 }
113 
114 const struct ogg_codec ff_dirac_codec = {
115  .magic = "BBCD\0",
116  .magicsize = 5,
117  .header = dirac_header,
118  .gptopts = dirac_gptopts,
119  .granule_is_start = 1,
120  .nb_header = 1,
121 };
122 
124  .magic = "KW-DIRAC",
125  .magicsize = 8,
126  .header = old_dirac_header,
127  .gptopts = old_dirac_gptopts,
128  .granule_is_start = 1,
129  .nb_header = 1,
130 };
enum AVColorTransferCharacteristic color_trc
Definition: avcodec.h:3547
enum AVColorRange color_range
Definition: dirac.h:59
Copyright (C) 2005 Michael Ahlberg, Måns Rullgård.
Definition: oggdec.h:31
unsigned int pflags
Definition: oggdec.h:67
static uint64_t dirac_gptopts(AVFormatContext *s, int idx, uint64_t granule, int64_t *dts_out)
Definition: oggparsedirac.c:64
enum AVColorTransferCharacteristic color_trc
Definition: dirac.h:61
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:2986
static uint64_t old_dirac_gptopts(AVFormatContext *s, int idx, uint64_t gp, int64_t *dts)
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
int num
numerator
Definition: rational.h:44
enum AVColorSpace color_space
Definition: avcodec.h:3548
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:387
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
Format I/O context.
Definition: avformat.h:940
unsigned int psize
Definition: oggdec.h:66
static int old_dirac_header(AVFormatContext *s, int idx)
Definition: oggparsedirac.c:84
uint8_t
int width
Video only.
Definition: avcodec.h:3525
Interface to Dirac Decoder/Encoder.
#define AV_RB32
Definition: intreadwrite.h:130
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
AVRational framerate
Definition: dirac.h:55
enum AVColorPrimaries color_primaries
Definition: avcodec.h:3546
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
uint64_t granule
Definition: oggdec.h:70
enum AVColorSpace colorspace
Definition: dirac.h:62
const struct ogg_codec ff_dirac_codec
unsigned int pstart
Definition: oggdec.h:65
struct ogg_stream * streams
Definition: oggdec.h:97
enum AVColorRange color_range
Video only.
Definition: avcodec.h:3545
Stream structure.
Definition: avformat.h:705
enum AVPixelFormat pix_fmt
Definition: dirac.h:58
unsigned height
Definition: dirac.h:35
static int64_t pts
Global timestamp for the audio frames.
const int8_t * magic
Definition: oggdec.h:32
uint8_t * buf
Definition: oggdec.h:62
static int dirac_header(AVFormatContext *s, int idx)
Definition: oggparsedirac.c:27
Main libavformat public API header.
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition: avcodec.h:3519
int den
denominator
Definition: rational.h:45
Definition: oggdec.h:96
void * priv_data
Format private data.
Definition: avformat.h:968
enum AVColorPrimaries color_primaries
Definition: dirac.h:60
AVCodecParameters * codecpar
Definition: avformat.h:831
unsigned width
Definition: dirac.h:34
const struct ogg_codec ff_old_dirac_codec