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 "libavutil/imgutils.h"
22 #include "libavutil/intreadwrite.h"
23 #include "libavcodec/dirac.h"
24 #include "avformat.h"
25 #include "internal.h"
26 #include "oggdec.h"
27 
28 static int dirac_header(AVFormatContext *s, int idx)
29 {
30  struct ogg *ogg = s->priv_data;
31  struct ogg_stream *os = ogg->streams + idx;
32  AVStream *st = s->streams[idx];
33  AVDiracSeqHeader *dsh;
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 = av_dirac_parse_sequence_header(&dsh, os->buf + os->pstart + 13, (os->psize - 13), s);
41  if (ret < 0)
42  return ret;
43 
46  st->codec->width = dsh->width;
47  st->codec->height = dsh->height;
48  st->codec->pix_fmt = dsh->pix_fmt;
49  st->codec->color_range = dsh->color_range;
50  st->codec->color_trc = dsh->color_trc;
52  st->codec->colorspace = dsh->colorspace;
53  st->codec->profile = dsh->profile;
54  st->codec->level = dsh->level;
55  if (av_image_check_sar(st->codec->width, st->codec->height, dsh->sample_aspect_ratio) >= 0)
57 
58  // dirac in ogg always stores timestamps as though the video were interlaced
59  avpriv_set_pts_info(st, 64, dsh->framerate.den, 2 * dsh->framerate.num);
60 
61  av_freep(&dsh);
62  return 1;
63 }
64 
65 // various undocument things: granule is signed (only for dirac!)
66 static uint64_t dirac_gptopts(AVFormatContext *s, int idx, uint64_t granule,
67  int64_t *dts_out)
68 {
69  int64_t gp = granule;
70  struct ogg *ogg = s->priv_data;
71  struct ogg_stream *os = ogg->streams + idx;
72 
73  unsigned dist = ((gp >> 14) & 0xff00) | (gp & 0xff);
74  int64_t dts = (gp >> 31);
75  int64_t pts = dts + ((gp >> 9) & 0x1fff);
76 
77  if (!dist)
78  os->pflags |= AV_PKT_FLAG_KEY;
79 
80  if (dts_out)
81  *dts_out = dts;
82 
83  return pts;
84 }
85 
86 static int old_dirac_header(AVFormatContext *s, int idx)
87 {
88  struct ogg *ogg = s->priv_data;
89  struct ogg_stream *os = ogg->streams + idx;
90  AVStream *st = s->streams[idx];
91  uint8_t *buf = os->buf + os->pstart;
92 
93  if (buf[0] != 'K')
94  return 0;
95 
98  avpriv_set_pts_info(st, 64, AV_RB32(buf+12), AV_RB32(buf+8));
99  return 1;
100 }
101 
102 static uint64_t old_dirac_gptopts(AVFormatContext *s, int idx, uint64_t gp,
103  int64_t *dts)
104 {
105  struct ogg *ogg = s->priv_data;
106  struct ogg_stream *os = ogg->streams + idx;
107  uint64_t iframe = gp >> 30;
108  uint64_t pframe = gp & 0x3fffffff;
109 
110  if (!pframe)
111  os->pflags |= AV_PKT_FLAG_KEY;
112 
113  return iframe + pframe;
114 }
115 
116 const struct ogg_codec ff_dirac_codec = {
117  .magic = "BBCD\0",
118  .magicsize = 5,
119  .header = dirac_header,
120  .gptopts = dirac_gptopts,
121  .granule_is_start = 1,
122  .nb_header = 1,
123 };
124 
126  .magic = "KW-DIRAC",
127  .magicsize = 8,
128  .header = old_dirac_header,
129  .gptopts = old_dirac_gptopts,
130  .granule_is_start = 1,
131  .nb_header = 1,
132 };
const char * s
Definition: avisynth_c.h:631
enum AVColorRange color_range
Definition: dirac.h:107
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:66
misc image utilities
enum AVColorTransferCharacteristic color_trc
Definition: dirac.h:109
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:4149
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2262
static uint64_t old_dirac_gptopts(AVFormatContext *s, int idx, uint64_t gp, int64_t *dts)
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:949
int num
numerator
Definition: rational.h:44
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1752
int profile
profile
Definition: avcodec.h:3028
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:398
Format I/O context.
Definition: avformat.h:1314
unsigned int psize
Definition: oggdec.h:66
static int old_dirac_header(AVFormatContext *s, int idx)
Definition: oggparsedirac.c:86
uint8_t
Interface to Dirac Decoder/Encoder.
AVRational sample_aspect_ratio
Definition: dirac.h:104
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:1382
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:266
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1499
AVRational framerate
Definition: dirac.h:103
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:896
uint64_t granule
Definition: oggdec.h:70
enum AVColorSpace colorspace
Definition: dirac.h:110
int width
picture width / height.
Definition: avcodec.h:1711
const struct ogg_codec ff_dirac_codec
unsigned int pstart
Definition: oggdec.h:65
struct ogg_stream * streams
Definition: oggdec.h:102
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2241
int level
level
Definition: avcodec.h:3117
Stream structure.
Definition: avformat.h:877
enum AVMediaType codec_type
Definition: avcodec.h:1540
enum AVCodecID codec_id
Definition: avcodec.h:1549
enum AVPixelFormat pix_fmt
Definition: dirac.h:106
void * buf
Definition: avisynth_c.h:553
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2255
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2248
unsigned height
Definition: dirac.h:83
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:28
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:1342
#define av_freep(p)
enum AVColorPrimaries color_primaries
Definition: dirac.h:108
#define gp
Definition: regdef.h:62
unsigned width
Definition: dirac.h:82
const struct ogg_codec ff_old_dirac_codec