FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "libavcodec/get_bits.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  dirac_source_params source;
33  GetBitContext gb;
34  int ret;
35 
36  // already parsed the header
37  if (st->codec->codec_id == AV_CODEC_ID_DIRAC)
38  return 0;
39 
40  ret = init_get_bits8(&gb, os->buf + os->pstart + 13, (os->psize - 13));
41  if (ret < 0)
42  return ret;
43 
44  ret = avpriv_dirac_parse_sequence_header(st->codec, &gb, &source);
45  if (ret < 0)
46  return ret;
47 
50  // dirac in ogg always stores timestamps as though the video were interlaced
52  return 1;
53 }
54 
55 // various undocument things: granule is signed (only for dirac!)
56 static uint64_t dirac_gptopts(AVFormatContext *s, int idx, uint64_t granule,
57  int64_t *dts_out)
58 {
59  int64_t gp = granule;
60  struct ogg *ogg = s->priv_data;
61  struct ogg_stream *os = ogg->streams + idx;
62 
63  unsigned dist = ((gp >> 14) & 0xff00) | (gp & 0xff);
64  int64_t dts = (gp >> 31);
65  int64_t pts = dts + ((gp >> 9) & 0x1fff);
66 
67  if (!dist)
68  os->pflags |= AV_PKT_FLAG_KEY;
69 
70  if (dts_out)
71  *dts_out = dts;
72 
73  return pts;
74 }
75 
76 static int old_dirac_header(AVFormatContext *s, int idx)
77 {
78  struct ogg *ogg = s->priv_data;
79  struct ogg_stream *os = ogg->streams + idx;
80  AVStream *st = s->streams[idx];
81  uint8_t *buf = os->buf + os->pstart;
82 
83  if (buf[0] != 'K')
84  return 0;
85 
88  avpriv_set_pts_info(st, 64, AV_RB32(buf+12), AV_RB32(buf+8));
89  return 1;
90 }
91 
92 static uint64_t old_dirac_gptopts(AVFormatContext *s, int idx, uint64_t gp,
93  int64_t *dts)
94 {
95  struct ogg *ogg = s->priv_data;
96  struct ogg_stream *os = ogg->streams + idx;
97  uint64_t iframe = gp >> 30;
98  uint64_t pframe = gp & 0x3fffffff;
99 
100  if (!pframe)
101  os->pflags |= AV_PKT_FLAG_KEY;
102 
103  return iframe + pframe;
104 }
105 
106 const struct ogg_codec ff_dirac_codec = {
107  .magic = "BBCD\0",
108  .magicsize = 5,
109  .header = dirac_header,
110  .gptopts = dirac_gptopts,
111  .granule_is_start = 1,
112  .nb_header = 1,
113 };
114 
116  .magic = "KW-DIRAC",
117  .magicsize = 8,
118  .header = old_dirac_header,
119  .gptopts = old_dirac_gptopts,
120  .granule_is_start = 1,
121  .nb_header = 1,
122 };
AVRational framerate
Definition: avcodec.h:3302
const char * s
Definition: avisynth_c.h:631
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:56
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:4083
static uint64_t old_dirac_gptopts(AVFormatContext *s, int idx, uint64_t gp, int64_t *dts)
Definition: oggparsedirac.c:92
int num
numerator
Definition: rational.h:44
Format I/O context.
Definition: avformat.h:1273
unsigned int psize
Definition: oggdec.h:66
static int old_dirac_header(AVFormatContext *s, int idx)
Definition: oggparsedirac.c:76
uint8_t
Interface to Dirac Decoder/Encoder.
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:87
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1341
bitstream reader API header.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1469
int avpriv_dirac_parse_sequence_header(AVCodecContext *avctx, GetBitContext *gb, dirac_source_params *source)
Definition: dirac.c:292
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
uint64_t granule
Definition: oggdec.h:70
const struct ogg_codec ff_dirac_codec
unsigned int pstart
Definition: oggdec.h:65
struct ogg_stream * streams
Definition: oggdec.h:102
Stream structure.
Definition: avformat.h:842
enum AVMediaType codec_type
Definition: avcodec.h:1510
enum AVCodecID codec_id
Definition: avcodec.h:1519
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:441
void * buf
Definition: avisynth_c.h:553
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 den
denominator
Definition: rational.h:45
Definition: oggdec.h:101
void * priv_data
Format private data.
Definition: avformat.h:1301
#define gp
Definition: regdef.h:62
const struct ogg_codec ff_old_dirac_codec