Mercurial > libavformat.hg
annotate oggenc.c @ 6114:10c9d304794f libavformat
Do not attempt to seek to index for streamed files and if the user explicitly
requested us not to read the index.
author | reimar |
---|---|
date | Tue, 08 Jun 2010 19:31:08 +0000 |
parents | a198eb01f895 |
children | 5c17c20dd67a |
rev | line source |
---|---|
2731 | 1 /* |
2 * Ogg muxer | |
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 | |
3286 | 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 | 24 #include "libavcodec/xiph.h" |
25 #include "libavcodec/bytestream.h" | |
4578 | 26 #include "libavcodec/flac.h" |
2731 | 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 | 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 | 43 typedef struct { |
44 unsigned page_counter; | |
45 uint8_t *header[3]; | |
46 int header_len[3]; | |
47 /** for theora granule */ | |
48 int kfgshift; | |
49 int64_t last_kf_pts; | |
50 int vrev; | |
3021 | 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 | 55 } OGGStreamContext; |
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 | 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 | 73 } |
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 | 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 | 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 | 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 | 191 } |
192 | |
5280 | 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 | 195 { |
196 const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT; | |
197 int size; | |
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 | 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 | 202 p = av_mallocz(size); |
203 if (!p) | |
204 return NULL; | |
205 p0 = p; | |
206 | |
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 | 209 |
210 *header_len = size; | |
211 return p0; | |
212 } | |
213 | |
4578 | 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 | 217 { |
4578 | 218 enum FLACExtradataFormat format; |
219 uint8_t *streaminfo; | |
2731 | 220 uint8_t *p; |
5054 | 221 |
4578 | 222 if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo)) |
2731 | 223 return -1; |
5054 | 224 |
225 // first packet: STREAMINFO | |
3188 | 226 oggstream->header_len[0] = 51; |
227 oggstream->header[0] = av_mallocz(51); // per ogg flac specs | |
2731 | 228 p = oggstream->header[0]; |
5053 | 229 if (!p) |
5930
08cd1179a20d
Replace all remaining occurrences of AVERROR_NOMEM with
stefano
parents:
5913
diff
changeset
|
230 return AVERROR(ENOMEM); |
2731 | 231 bytestream_put_byte(&p, 0x7F); |
232 bytestream_put_buffer(&p, "FLAC", 4); | |
233 bytestream_put_byte(&p, 1); // major version | |
234 bytestream_put_byte(&p, 0); // minor version | |
235 bytestream_put_be16(&p, 1); // headers packets without this one | |
236 bytestream_put_buffer(&p, "fLaC", 4); | |
237 bytestream_put_byte(&p, 0x00); // streaminfo | |
238 bytestream_put_be24(&p, 34); | |
4578 | 239 bytestream_put_buffer(&p, streaminfo, FLAC_STREAMINFO_SIZE); |
5054 | 240 |
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 | 243 if (!p) |
5930
08cd1179a20d
Replace all remaining occurrences of AVERROR_NOMEM with
stefano
parents:
5913
diff
changeset
|
244 return AVERROR(ENOMEM); |
5280 | 245 oggstream->header[1] = p; |
2731 | 246 bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment |
247 bytestream_put_be24(&p, oggstream->header_len[1] - 4); | |
5280 | 248 |
249 return 0; | |
250 } | |
251 | |
252 #define SPEEX_HEADER_SIZE 80 | |
253 | |
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 | 257 { |
258 uint8_t *p; | |
259 | |
260 if (avctx->extradata_size < SPEEX_HEADER_SIZE) | |
261 return -1; | |
262 | |
263 // first packet: Speex header | |
264 p = av_mallocz(SPEEX_HEADER_SIZE); | |
265 if (!p) | |
5930
08cd1179a20d
Replace all remaining occurrences of AVERROR_NOMEM with
stefano
parents:
5913
diff
changeset
|
266 return AVERROR(ENOMEM); |
5280 | 267 oggstream->header[0] = p; |
268 oggstream->header_len[0] = SPEEX_HEADER_SIZE; | |
269 bytestream_put_buffer(&p, avctx->extradata, SPEEX_HEADER_SIZE); | |
270 AV_WL32(&oggstream->header[0][68], 0); // set extra_headers to 0 | |
271 | |
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 | 274 if (!p) |
5930
08cd1179a20d
Replace all remaining occurrences of AVERROR_NOMEM with
stefano
parents:
5913
diff
changeset
|
275 return AVERROR(ENOMEM); |
5280 | 276 oggstream->header[1] = p; |
5054 | 277 |
2731 | 278 return 0; |
279 } | |
280 | |
281 static int ogg_write_header(AVFormatContext *s) | |
282 { | |
283 OGGStreamContext *oggstream; | |
284 int i, j; | |
6054
a198eb01f895
In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents:
6028
diff
changeset
|
285 |
2731 | 286 for (i = 0; i < s->nb_streams; i++) { |
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 | 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 | 293 av_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den); |
294 if (st->codec->codec_id != CODEC_ID_VORBIS && | |
295 st->codec->codec_id != CODEC_ID_THEORA && | |
5280 | 296 st->codec->codec_id != CODEC_ID_SPEEX && |
2731 | 297 st->codec->codec_id != CODEC_ID_FLAC) { |
298 av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i); | |
299 return -1; | |
300 } | |
301 | |
302 if (!st->codec->extradata || !st->codec->extradata_size) { | |
303 av_log(s, AV_LOG_ERROR, "No extradata present\n"); | |
304 return -1; | |
305 } | |
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 | 320 st->priv_data = oggstream; |
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 | 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 | 329 } |
5280 | 330 } else if (st->codec->codec_id == CODEC_ID_SPEEX) { |
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 | 334 if (err) { |
335 av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n"); | |
336 av_freep(&st->priv_data); | |
337 return err; | |
338 } | |
2731 | 339 } else { |
340 if (ff_split_xiph_headers(st->codec->extradata, st->codec->extradata_size, | |
341 st->codec->codec_id == CODEC_ID_VORBIS ? 30 : 42, | |
342 oggstream->header, oggstream->header_len) < 0) { | |
343 av_log(s, AV_LOG_ERROR, "Extradata corrupted\n"); | |
344 av_freep(&st->priv_data); | |
345 return -1; | |
346 } | |
347 if (st->codec->codec_id == CODEC_ID_THEORA) { | |
348 /** KFGSHIFT is the width of the less significant section of the granule position | |
349 The less significant section is the frame count since the last keyframe */ | |
350 oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5); | |
351 oggstream->vrev = oggstream->header[0][9]; | |
352 av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n", | |
353 oggstream->kfgshift, oggstream->vrev); | |
354 } | |
355 } | |
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 | 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 | 374 } |
375 return 0; | |
376 } | |
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 | 400 static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt) |
401 { | |
402 AVStream *st = s->streams[pkt->stream_index]; | |
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 | 405 int64_t granule; |
406 | |
407 if (st->codec->codec_id == CODEC_ID_THEORA) { | |
408 int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration; | |
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 | 411 oggstream->last_kf_pts = pts; |
412 pframe_count = pts - oggstream->last_kf_pts; | |
413 // prevent frame count from overflow if key frame flag is not set | |
414 if (pframe_count >= (1<<oggstream->kfgshift)) { | |
415 oggstream->last_kf_pts += pframe_count; | |
416 pframe_count = 0; | |
417 } | |
418 granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count; | |
419 } else | |
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 | 427 |
428 return 0; | |
429 } | |
430 | |
431 static int ogg_write_trailer(AVFormatContext *s) | |
432 { | |
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 | 441 for (i = 0; i < s->nb_streams; i++) { |
442 AVStream *st = s->streams[i]; | |
443 OGGStreamContext *oggstream = st->priv_data; | |
5280 | 444 if (st->codec->codec_id == CODEC_ID_FLAC || |
445 st->codec->codec_id == CODEC_ID_SPEEX) { | |
2731 | 446 av_free(oggstream->header[0]); |
447 av_free(oggstream->header[1]); | |
448 } | |
449 av_freep(&st->priv_data); | |
450 } | |
451 return 0; | |
452 } | |
453 | |
454 AVOutputFormat ogg_muxer = { | |
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 | 457 "application/ogg", |
5280 | 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 | 460 CODEC_ID_FLAC, |
461 CODEC_ID_THEORA, | |
462 ogg_write_header, | |
463 ogg_write_packet, | |
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 | 466 }; |