FFmpeg
Loading...
Searching...
No Matches
theora.c
Go to the documentation of this file.
1/*
2 * Ogg Theora muxer mapping
3 * Copyright (c) 2007 Baptiste Coudurier <baptiste dot coudurier at free dot fr>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "libavcodec/xiph.h"
24#include "libavutil/log.h"
25#include "libavutil/rational.h"
28
29typedef struct TheoraMuxerContext {
30 int vrev; ///< bitstream revision
33
35{
36 OGGStreamContext *oggstream = st->priv_data;
37 TheoraMuxerContext *theora = oggstream->priv_data;
38 int den, num;
39 int err = ff_ogg_build_xiph_headers(s, st, 42, 0x81, "theora", 0);
40
41 if (err < 0)
42 return err;
43
44 den = AV_RB32(oggstream->header[0] + 22);
45 num = AV_RB32(oggstream->header[0] + 26);
46 /* Make sure to use time base stored in the Theora stream header to write
47 correct timestamps */
48 if (st->time_base.num != num || st->time_base.den != den) {
49 av_log(s, AV_LOG_DEBUG, "Changing time base from %d/%d to %d/%d\n",
50 st->time_base.num, st->time_base.den, num, den);
51 avpriv_set_pts_info(st, 64, num, den);
52 }
53 /** KFGSHIFT is the width of the less significant section of the granule position
54 The less significant section is the frame count since the last keyframe */
55 oggstream->granule_shift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
56 oggstream->granule_delta_mask = (INT64_C(1) << oggstream->granule_shift) - 1;
57 oggstream->granule_key_mask = oggstream->granule_delta_mask;
58 theora->vrev = oggstream->header[0][9];
59 av_log(s, AV_LOG_DEBUG, "theora kfgshift %u, vrev %d\n",
60 oggstream->granule_shift, theora->vrev);
61
62 return 0;
63}
64
65/**
66 * Whether Theora headers set a frame rate other than the stream time base,
67 * which packets keep across links.
68 */
69static int theora_rate_changed(const AVStream *st, const uint8_t *extradata, size_t size)
70{
71 const uint8_t *header[3];
72 int header_len[3];
73
74 if (size > INT_MAX ||
75 avpriv_split_xiph_headers(extradata, size, 42, header, header_len) < 0 ||
76 header_len[0] < 42)
77 return 0;
78 return av_cmp_q(st->time_base,
79 (AVRational){ AV_RB32(header[0] + 26), AV_RB32(header[0] + 22) }) != 0;
80}
81
82static const char *theora_new_link_refused(const AVStream *st, const AVPacket *pkt,
83 const uint8_t *new_extradata, size_t size)
84{
85 if (!new_extradata && !(pkt->flags & AV_PKT_FLAG_KEY))
86 return "New Theora links must start on a keyframe";
87 if (new_extradata && theora_rate_changed(st, new_extradata, size))
88 return "New Theora links cannot change the frame rate";
89 return NULL;
90}
91
93{
94 OGGStreamContext *oggstream = st->priv_data;
95 TheoraMuxerContext *theora = oggstream->priv_data;
96 int64_t pts = theora->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
97 int pframe_count;
98
99 if (pkt->flags & AV_PKT_FLAG_KEY)
100 theora->last_kf_pts = pts;
101 pframe_count = pts - theora->last_kf_pts;
102 // prevent frame count from overflow if key frame flag is not set
103 if (pframe_count >= (INT64_C(1)<<oggstream->granule_shift)) {
104 theora->last_kf_pts += pframe_count;
105 pframe_count = 0;
106 }
107 return (theora->last_kf_pts<<oggstream->granule_shift) | pframe_count;
108}
109
111 .codec_id = AV_CODEC_ID_THEORA,
112 .priv_data_size = sizeof(TheoraMuxerContext),
113 .new_links = 1,
114 .init = theora_init,
115 .new_link_refused = theora_new_link_refused,
116 .packet_granule = theora_packet_granule,
117};
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:865
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
@ AV_CODEC_ID_THEORA
Definition codec_id.h:80
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:657
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition rational.h:89
#define AV_RB32(p)
static const char * theora_new_link_refused(const AVStream *st, const AVPacket *pkt, const uint8_t *new_extradata, size_t size)
Definition theora.c:82
static int theora_rate_changed(const AVStream *st, const uint8_t *extradata, size_t size)
Whether Theora headers set a frame rate other than the stream time base, which packets keep across li...
Definition theora.c:69
static int theora_init(AVFormatContext *s, AVStream *st)
Definition theora.c:34
static int64_t theora_packet_granule(AVStream *st, const AVPacket *pkt)
Definition theora.c:92
int ff_ogg_build_xiph_headers(AVFormatContext *s, AVStream *st, int first_header_size, int comment_type, const char *name, int framing_bit)
Build the headers of a codec using the Xiph header layout: identification, comment and setup packets ...
Definition muxer.c:287
const OGGMuxerMapping ff_ogg_theora_muxer_mapping
Definition theora.c:110
Utilities for rational number calculation.
static const uint8_t header[24]
Definition sdr2.c:68
Format I/O context.
Definition avformat.h:1401
This structure stores compressed data.
Definition packet.h:587
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:768
void * priv_data
Definition avformat.h:793
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:807
Codec mapping of the muxer: the parts of the Ogg encapsulation that depend on the codec.
Definition muxer.h:71
unsigned granule_shift
Granule layout, set by the mapping.
Definition muxer.h:55
uint8_t * header[3]
Definition muxer.h:47
void * priv_data
state of the mapping
Definition muxer.h:50
int64_t granule_delta_mask
Definition muxer.h:56
int64_t granule_key_mask
bits all clear on keyframes, 0 when no granule marks one
Definition muxer.h:57
int64_t last_kf_pts
Definition theora.c:31
int vrev
bitstream revision
Definition theora.c:30
#define av_log(a,...)
int num
Definition error.c:23
static int64_t pts
int size
int avpriv_split_xiph_headers(const uint8_t *extradata, int extradata_size, int first_header_size, const uint8_t *header_start[3], int header_len[3])
Split a single extradata buffer into the three headers that most Xiph codecs use.
Definition xiph.c:26