annotate oggenc.c @ 6054:a198eb01f895 libavformat

In ogg muxer, use random serial number of each ogg streams
author bcoudurier
date Mon, 24 May 2010 23:37:33 +0000
parents d920f5eb9d10
children 5c17c20dd67a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
1 /*
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
2 * Ogg muxer
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
3 * Copyright (c) 2007 Baptiste Coudurier <baptiste dot coudurier at free dot fr>
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
4 *
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
5 * This file is part of FFmpeg.
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
6 *
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
11 *
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
15 * Lesser General Public License for more details.
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
16 *
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
20 */
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
21
3286
6f61c3b36632 Use full path for #includes from another directory.
diego
parents: 3188
diff changeset
22 #include "libavutil/crc.h"
6054
a198eb01f895 In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents: 6028
diff changeset
23 #include "libavutil/random_seed.h"
3286
6f61c3b36632 Use full path for #includes from another directory.
diego
parents: 3188
diff changeset
24 #include "libavcodec/xiph.h"
6f61c3b36632 Use full path for #includes from another directory.
diego
parents: 3188
diff changeset
25 #include "libavcodec/bytestream.h"
4578
c05d167a4fe2 Use a shared function to validate FLAC extradata.
jbr
parents: 4418
diff changeset
26 #include "libavcodec/flac.h"
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
27 #include "avformat.h"
4418
d5119d75439d Move declaration of ff_interleave_add_packet to internal.h.
bcoudurier
parents: 4311
diff changeset
28 #include "internal.h"
5858
274fed269b59 Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents: 5851
diff changeset
29 #include "vorbiscomment.h"
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
30
6027
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
31 #define MAX_PAGE_SIZE 65025
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
32
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
33 typedef struct {
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
34 int64_t granule;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
35 int stream_index;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
36 uint8_t flags;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
37 uint8_t segments_count;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
38 uint8_t segments[255];
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
39 uint8_t data[MAX_PAGE_SIZE];
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
40 uint16_t size;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
41 } OGGPage;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
42
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
43 typedef struct {
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
44 unsigned page_counter;
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
45 uint8_t *header[3];
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
46 int header_len[3];
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
47 /** for theora granule */
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
48 int kfgshift;
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
49 int64_t last_kf_pts;
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
50 int vrev;
3021
6f393e79e896 Fix issue 338:
bcoudurier
parents: 3020
diff changeset
51 int eos;
6027
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
52 unsigned page_count; ///< number of page buffered
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
53 OGGPage page; ///< current page
6054
a198eb01f895 In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents: 6028
diff changeset
54 unsigned serial_num; ///< serial number
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
55 } OGGStreamContext;
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
56
6027
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
57 typedef struct OGGPageList {
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
58 OGGPage page;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
59 struct OGGPageList *next;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
60 } OGGPageList;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
61
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
62 typedef struct {
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
63 OGGPageList *page_list;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
64 } OGGContext;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
65
3973
549a09cf23fe Remove offset_t typedef and use int64_t directly instead.
diego
parents: 3593
diff changeset
66 static void ogg_update_checksum(AVFormatContext *s, int64_t crc_offset)
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
67 {
3973
549a09cf23fe Remove offset_t typedef and use int64_t directly instead.
diego
parents: 3593
diff changeset
68 int64_t pos = url_ftell(s->pb);
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2731
diff changeset
69 uint32_t checksum = get_checksum(s->pb);
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2731
diff changeset
70 url_fseek(s->pb, crc_offset, SEEK_SET);
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2731
diff changeset
71 put_be32(s->pb, checksum);
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2731
diff changeset
72 url_fseek(s->pb, pos, SEEK_SET);
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
73 }
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
74
6027
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
75 static void ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
76 {
6027
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
77 OGGStreamContext *oggstream = s->streams[page->stream_index]->priv_data;
3973
549a09cf23fe Remove offset_t typedef and use int64_t directly instead.
diego
parents: 3593
diff changeset
78 int64_t crc_offset;
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
79
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2731
diff changeset
80 init_checksum(s->pb, ff_crc04C11DB7_update, 0);
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2731
diff changeset
81 put_tag(s->pb, "OggS");
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2731
diff changeset
82 put_byte(s->pb, 0);
6027
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
83 put_byte(s->pb, page->flags | extra_flags);
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
84 put_le64(s->pb, page->granule);
6054
a198eb01f895 In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents: 6028
diff changeset
85 put_le32(s->pb, oggstream->serial_num);
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2731
diff changeset
86 put_le32(s->pb, oggstream->page_counter++);
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2731
diff changeset
87 crc_offset = url_ftell(s->pb);
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2731
diff changeset
88 put_le32(s->pb, 0); // crc
6027
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
89 put_byte(s->pb, page->segments_count);
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
90 put_buffer(s->pb, page->segments, page->segments_count);
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
91 put_buffer(s->pb, page->data, page->size);
5851
d88fc4640994 Correctly write last 0 lacing value when packet size is multiple of 255, patch by Greg Maxwell, gmaxwell at gmail dot com
bcoudurier
parents: 5602
diff changeset
92
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
93 ogg_update_checksum(s, crc_offset);
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2731
diff changeset
94 put_flush_packet(s->pb);
6027
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
95 oggstream->page_count--;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
96 }
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
97
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
98 static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, OGGPage *page)
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
99 {
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
100 if (oggstream->kfgshift)
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
101 return (page->granule>>oggstream->kfgshift) +
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
102 (page->granule & ((1<<oggstream->kfgshift)-1));
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
103 else
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
104 return page->granule;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
105 }
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
106
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
107 static int ogg_compare_granule(AVFormatContext *s, OGGPage *next, OGGPage *page)
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
108 {
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
109 AVStream *st2 = s->streams[next->stream_index];
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
110 AVStream *st = s->streams[page->stream_index];
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
111 int64_t next_granule, cur_granule;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
112
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
113 if (next->granule == -1 || page->granule == -1)
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
114 return 0;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
115
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
116 next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next),
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
117 st2->time_base, AV_TIME_BASE_Q);
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
118 cur_granule = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page),
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
119 st ->time_base, AV_TIME_BASE_Q);
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
120 return next_granule > cur_granule;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
121 }
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
122
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
123 static int ogg_reset_cur_page(OGGStreamContext *oggstream)
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
124 {
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
125 oggstream->page.granule = -1;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
126 oggstream->page.flags = 0;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
127 oggstream->page.segments_count = 0;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
128 oggstream->page.size = 0;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
129 return 0;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
130 }
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
131
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
132 static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream)
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
133 {
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
134 OGGContext *ogg = s->priv_data;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
135 OGGPageList **p = &ogg->page_list;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
136 OGGPageList *l = av_mallocz(sizeof(*l));
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
137
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
138 if (!l)
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
139 return AVERROR(ENOMEM);
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
140 l->page = oggstream->page;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
141
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
142 oggstream->page_count++;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
143 ogg_reset_cur_page(oggstream);
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
144
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
145 while (*p) {
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
146 if (ogg_compare_granule(s, &(*p)->page, &l->page))
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
147 break;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
148 p = &(*p)->next;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
149 }
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
150 l->next = *p;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
151 *p = l;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
152
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
153 return 0;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
154 }
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
155
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
156 static int ogg_buffer_data(AVFormatContext *s, AVStream *st,
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
157 uint8_t *data, unsigned size, int64_t granule)
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
158 {
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
159 OGGStreamContext *oggstream = st->priv_data;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
160 int total_segments = size / 255 + 1;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
161 uint8_t *p = data;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
162 int i, segments, len;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
163
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
164 for (i = 0; i < total_segments; ) {
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
165 OGGPage *page = &oggstream->page;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
166
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
167 segments = FFMIN(total_segments - i, 255 - page->segments_count);
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
168
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
169 if (i && !page->segments_count)
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
170 page->flags |= 1; // continued packet
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
171
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
172 memset(page->segments+page->segments_count, 255, segments - 1);
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
173 page->segments_count += segments - 1;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
174
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
175 len = FFMIN(size, segments*255);
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
176 page->segments[page->segments_count++] = len - (segments-1)*255;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
177 memcpy(page->data+page->size, p, len);
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
178 p += len;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
179 size -= len;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
180 i += segments;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
181 page->size += len;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
182
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
183 if (i == total_segments)
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
184 page->granule = granule;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
185
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
186 if (page->segments_count == 255) {
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
187 ogg_buffer_page(s, oggstream);
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
188 }
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
189 }
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
190 return 0;
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
191 }
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
192
5280
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
193 static uint8_t *ogg_write_vorbiscomment(int offset, int bitexact,
5858
274fed269b59 Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents: 5851
diff changeset
194 int *header_len, AVMetadata *m)
5280
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
195 {
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
196 const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
197 int size;
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
198 uint8_t *p, *p0;
5858
274fed269b59 Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents: 5851
diff changeset
199 unsigned int count;
5280
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
200
5858
274fed269b59 Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents: 5851
diff changeset
201 size = offset + ff_vorbiscomment_length(m, vendor, &count);
5280
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
202 p = av_mallocz(size);
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
203 if (!p)
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
204 return NULL;
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
205 p0 = p;
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
206
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
207 p += offset;
5858
274fed269b59 Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents: 5851
diff changeset
208 ff_vorbiscomment_write(&p, m, vendor, count);
5280
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
209
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
210 *header_len = size;
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
211 return p0;
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
212 }
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
213
4578
c05d167a4fe2 Use a shared function to validate FLAC extradata.
jbr
parents: 4418
diff changeset
214 static int ogg_build_flac_headers(AVCodecContext *avctx,
5858
274fed269b59 Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents: 5851
diff changeset
215 OGGStreamContext *oggstream, int bitexact,
274fed269b59 Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents: 5851
diff changeset
216 AVMetadata *m)
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
217 {
4578
c05d167a4fe2 Use a shared function to validate FLAC extradata.
jbr
parents: 4418
diff changeset
218 enum FLACExtradataFormat format;
c05d167a4fe2 Use a shared function to validate FLAC extradata.
jbr
parents: 4418
diff changeset
219 uint8_t *streaminfo;
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
220 uint8_t *p;
5054
6c7d0a04ae96 cosmetics: spacing and comments
jbr
parents: 5053
diff changeset
221
4578
c05d167a4fe2 Use a shared function to validate FLAC extradata.
jbr
parents: 4418
diff changeset
222 if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo))
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
223 return -1;
5054
6c7d0a04ae96 cosmetics: spacing and comments
jbr
parents: 5053
diff changeset
224
6c7d0a04ae96 cosmetics: spacing and comments
jbr
parents: 5053
diff changeset
225 // first packet: STREAMINFO
3188
87119f0c4736 fix Ogg/FLAC header size
jbr
parents: 3021
diff changeset
226 oggstream->header_len[0] = 51;
87119f0c4736 fix Ogg/FLAC header size
jbr
parents: 3021
diff changeset
227 oggstream->header[0] = av_mallocz(51); // per ogg flac specs
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
228 p = oggstream->header[0];
5053
037f1082ecd6 oggenc: check for failed av_mallocz()
jbr
parents: 4578
diff changeset
229 if (!p)
5930
08cd1179a20d Replace all remaining occurrences of AVERROR_NOMEM with
stefano
parents: 5913
diff changeset
230 return AVERROR(ENOMEM);
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
231 bytestream_put_byte(&p, 0x7F);
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
232 bytestream_put_buffer(&p, "FLAC", 4);
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
233 bytestream_put_byte(&p, 1); // major version
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
234 bytestream_put_byte(&p, 0); // minor version
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
235 bytestream_put_be16(&p, 1); // headers packets without this one
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
236 bytestream_put_buffer(&p, "fLaC", 4);
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
237 bytestream_put_byte(&p, 0x00); // streaminfo
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
238 bytestream_put_be24(&p, 34);
4578
c05d167a4fe2 Use a shared function to validate FLAC extradata.
jbr
parents: 4418
diff changeset
239 bytestream_put_buffer(&p, streaminfo, FLAC_STREAMINFO_SIZE);
5054
6c7d0a04ae96 cosmetics: spacing and comments
jbr
parents: 5053
diff changeset
240
6c7d0a04ae96 cosmetics: spacing and comments
jbr
parents: 5053
diff changeset
241 // second packet: VorbisComment
5858
274fed269b59 Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents: 5851
diff changeset
242 p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1], m);
5053
037f1082ecd6 oggenc: check for failed av_mallocz()
jbr
parents: 4578
diff changeset
243 if (!p)
5930
08cd1179a20d Replace all remaining occurrences of AVERROR_NOMEM with
stefano
parents: 5913
diff changeset
244 return AVERROR(ENOMEM);
5280
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
245 oggstream->header[1] = p;
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
246 bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
247 bytestream_put_be24(&p, oggstream->header_len[1] - 4);
5280
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
248
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
249 return 0;
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
250 }
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
251
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
252 #define SPEEX_HEADER_SIZE 80
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
253
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
254 static int ogg_build_speex_headers(AVCodecContext *avctx,
5858
274fed269b59 Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents: 5851
diff changeset
255 OGGStreamContext *oggstream, int bitexact,
274fed269b59 Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents: 5851
diff changeset
256 AVMetadata *m)
5280
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
257 {
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
258 uint8_t *p;
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
259
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
260 if (avctx->extradata_size < SPEEX_HEADER_SIZE)
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
261 return -1;
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
262
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
263 // first packet: Speex header
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
264 p = av_mallocz(SPEEX_HEADER_SIZE);
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
265 if (!p)
5930
08cd1179a20d Replace all remaining occurrences of AVERROR_NOMEM with
stefano
parents: 5913
diff changeset
266 return AVERROR(ENOMEM);
5280
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
267 oggstream->header[0] = p;
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
268 oggstream->header_len[0] = SPEEX_HEADER_SIZE;
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
269 bytestream_put_buffer(&p, avctx->extradata, SPEEX_HEADER_SIZE);
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
270 AV_WL32(&oggstream->header[0][68], 0); // set extra_headers to 0
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
271
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
272 // second packet: VorbisComment
5858
274fed269b59 Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents: 5851
diff changeset
273 p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1], m);
5280
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
274 if (!p)
5930
08cd1179a20d Replace all remaining occurrences of AVERROR_NOMEM with
stefano
parents: 5913
diff changeset
275 return AVERROR(ENOMEM);
5280
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
276 oggstream->header[1] = p;
5054
6c7d0a04ae96 cosmetics: spacing and comments
jbr
parents: 5053
diff changeset
277
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
278 return 0;
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
279 }
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
280
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
281 static int ogg_write_header(AVFormatContext *s)
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
282 {
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
283 OGGStreamContext *oggstream;
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
284 int i, j;
6054
a198eb01f895 In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents: 6028
diff changeset
285
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
286 for (i = 0; i < s->nb_streams; i++) {
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
287 AVStream *st = s->streams[i];
6054
a198eb01f895 In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents: 6028
diff changeset
288 unsigned serial_num = i;
a198eb01f895 In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents: 6028
diff changeset
289
5910
536e5527c1e0 Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 5858
diff changeset
290 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
291 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
5910
536e5527c1e0 Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 5858
diff changeset
292 else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
293 av_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den);
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
294 if (st->codec->codec_id != CODEC_ID_VORBIS &&
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
295 st->codec->codec_id != CODEC_ID_THEORA &&
5280
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
296 st->codec->codec_id != CODEC_ID_SPEEX &&
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
297 st->codec->codec_id != CODEC_ID_FLAC) {
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
298 av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
299 return -1;
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
300 }
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
301
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
302 if (!st->codec->extradata || !st->codec->extradata_size) {
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
303 av_log(s, AV_LOG_ERROR, "No extradata present\n");
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
304 return -1;
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
305 }
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
306 oggstream = av_mallocz(sizeof(*oggstream));
6027
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
307 oggstream->page.stream_index = i;
6054
a198eb01f895 In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents: 6028
diff changeset
308
a198eb01f895 In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents: 6028
diff changeset
309 if (!(st->codec->flags & CODEC_FLAG_BITEXACT))
a198eb01f895 In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents: 6028
diff changeset
310 do {
a198eb01f895 In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents: 6028
diff changeset
311 serial_num = av_get_random_seed();
a198eb01f895 In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents: 6028
diff changeset
312 for (j = 0; j < i; j++) {
a198eb01f895 In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents: 6028
diff changeset
313 OGGStreamContext *sc = s->streams[j]->priv_data;
a198eb01f895 In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents: 6028
diff changeset
314 if (serial_num == sc->serial_num)
a198eb01f895 In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents: 6028
diff changeset
315 break;
a198eb01f895 In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents: 6028
diff changeset
316 }
a198eb01f895 In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents: 6028
diff changeset
317 } while (j < i);
a198eb01f895 In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents: 6028
diff changeset
318 oggstream->serial_num = serial_num;
a198eb01f895 In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents: 6028
diff changeset
319
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
320 st->priv_data = oggstream;
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
321 if (st->codec->codec_id == CODEC_ID_FLAC) {
5055
5a6bbdd352fb oggenc: return error value from ogg_build_flac_headers()
jbr
parents: 5054
diff changeset
322 int err = ogg_build_flac_headers(st->codec, oggstream,
5858
274fed269b59 Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents: 5851
diff changeset
323 st->codec->flags & CODEC_FLAG_BITEXACT,
274fed269b59 Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents: 5851
diff changeset
324 s->metadata);
5055
5a6bbdd352fb oggenc: return error value from ogg_build_flac_headers()
jbr
parents: 5054
diff changeset
325 if (err) {
5056
f67fad61077e oggenc: Change error log text. An error here does not necessarily mean
jbr
parents: 5055
diff changeset
326 av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
327 av_freep(&st->priv_data);
5055
5a6bbdd352fb oggenc: return error value from ogg_build_flac_headers()
jbr
parents: 5054
diff changeset
328 return err;
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
329 }
5280
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
330 } else if (st->codec->codec_id == CODEC_ID_SPEEX) {
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
331 int err = ogg_build_speex_headers(st->codec, oggstream,
5858
274fed269b59 Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents: 5851
diff changeset
332 st->codec->flags & CODEC_FLAG_BITEXACT,
274fed269b59 Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents: 5851
diff changeset
333 s->metadata);
5280
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
334 if (err) {
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
335 av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
336 av_freep(&st->priv_data);
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
337 return err;
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
338 }
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
339 } else {
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
340 if (ff_split_xiph_headers(st->codec->extradata, st->codec->extradata_size,
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
341 st->codec->codec_id == CODEC_ID_VORBIS ? 30 : 42,
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
342 oggstream->header, oggstream->header_len) < 0) {
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
343 av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
344 av_freep(&st->priv_data);
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
345 return -1;
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
346 }
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
347 if (st->codec->codec_id == CODEC_ID_THEORA) {
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
348 /** KFGSHIFT is the width of the less significant section of the granule position
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
349 The less significant section is the frame count since the last keyframe */
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
350 oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
351 oggstream->vrev = oggstream->header[0][9];
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
352 av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
353 oggstream->kfgshift, oggstream->vrev);
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
354 }
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
355 }
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
356 }
6027
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
357
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
358 for (j = 0; j < s->nb_streams; j++) {
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
359 OGGStreamContext *oggstream = s->streams[j]->priv_data;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
360 ogg_buffer_data(s, s->streams[j], oggstream->header[0],
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
361 oggstream->header_len[0], 0);
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
362 oggstream->page.flags |= 2; // bos
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
363 ogg_buffer_page(s, oggstream);
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
364 }
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
365 for (j = 0; j < s->nb_streams; j++) {
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
366 AVStream *st = s->streams[j];
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
367 OGGStreamContext *oggstream = st->priv_data;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
368 for (i = 1; i < 3; i++) {
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
369 if (oggstream && oggstream->header_len[i])
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
370 ogg_buffer_data(s, st, oggstream->header[i],
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
371 oggstream->header_len[i], 0);
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
372 }
6027
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
373 ogg_buffer_page(s, oggstream);
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
374 }
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
375 return 0;
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
376 }
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
377
6027
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
378 static void ogg_write_pages(AVFormatContext *s, int flush)
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
379 {
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
380 OGGContext *ogg = s->priv_data;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
381 OGGPageList *next, *p;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
382
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
383 if (!ogg->page_list)
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
384 return;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
385
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
386 for (p = ogg->page_list; p; ) {
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
387 OGGStreamContext *oggstream =
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
388 s->streams[p->page.stream_index]->priv_data;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
389 if (oggstream->page_count < 2 && !flush)
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
390 break;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
391 ogg_write_page(s, &p->page,
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
392 flush && oggstream->page_count == 1 ? 4 : 0); // eos
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
393 next = p->next;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
394 av_freep(&p);
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
395 p = next;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
396 }
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
397 ogg->page_list = p;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
398 }
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
399
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
400 static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
401 {
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
402 AVStream *st = s->streams[pkt->stream_index];
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
403 OGGStreamContext *oggstream = st->priv_data;
6027
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
404 int ret;
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
405 int64_t granule;
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
406
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
407 if (st->codec->codec_id == CODEC_ID_THEORA) {
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
408 int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
409 int pframe_count;
5913
11bb10c37225 Replace all occurences of PKT_FLAG_KEY with AV_PKT_FLAG_KEY.
cehoyos
parents: 5910
diff changeset
410 if (pkt->flags & AV_PKT_FLAG_KEY)
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
411 oggstream->last_kf_pts = pts;
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
412 pframe_count = pts - oggstream->last_kf_pts;
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
413 // prevent frame count from overflow if key frame flag is not set
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
414 if (pframe_count >= (1<<oggstream->kfgshift)) {
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
415 oggstream->last_kf_pts += pframe_count;
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
416 pframe_count = 0;
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
417 }
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
418 granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
419 } else
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
420 granule = pkt->pts + pkt->duration;
6027
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
421
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
422 ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule);
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
423 if (ret < 0)
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
424 return ret;
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
425
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
426 ogg_write_pages(s, 0);
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
427
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
428 return 0;
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
429 }
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
430
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
431 static int ogg_write_trailer(AVFormatContext *s)
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
432 {
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
433 int i;
6027
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
434
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
435 /* flush current page */
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
436 for (i = 0; i < s->nb_streams; i++)
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
437 ogg_buffer_page(s, s->streams[i]->priv_data);
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
438
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
439 ogg_write_pages(s, 1);
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
440
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
441 for (i = 0; i < s->nb_streams; i++) {
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
442 AVStream *st = s->streams[i];
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
443 OGGStreamContext *oggstream = st->priv_data;
5280
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
444 if (st->codec->codec_id == CODEC_ID_FLAC ||
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
445 st->codec->codec_id == CODEC_ID_SPEEX) {
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
446 av_free(oggstream->header[0]);
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
447 av_free(oggstream->header[1]);
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
448 }
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
449 av_freep(&st->priv_data);
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
450 }
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
451 return 0;
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
452 }
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
453
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
454 AVOutputFormat ogg_muxer = {
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
455 "ogg",
3424
7a0230981402 Make long_names in lavf/lavdev optional depending on CONFIG_SMALL.
diego
parents: 3286
diff changeset
456 NULL_IF_CONFIG_SMALL("Ogg"),
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
457 "application/ogg",
5280
4292c6f121dc Add Speex support to the Ogg muxer.
jbr
parents: 5210
diff changeset
458 "ogg,ogv,spx",
6027
710cc5866cbf In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents: 5930
diff changeset
459 sizeof(OGGContext),
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
460 CODEC_ID_FLAC,
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
461 CODEC_ID_THEORA,
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
462 ogg_write_header,
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
463 ogg_write_packet,
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
464 ogg_write_trailer,
5858
274fed269b59 Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents: 5851
diff changeset
465 .metadata_conv = ff_vorbiscomment_metadata_conv,
2731
6b43751d7264 ogg muxer
bcoudurier
parents:
diff changeset
466 };