Mercurial > libavformat.hg
annotate oggenc.c @ 6341:b7a16d6df72a libavformat
Add WebM to the Matroska demuxer name.
author | alexc |
---|---|
date | Sun, 01 Aug 2010 00:30:37 +0000 |
parents | f4f55ad4a603 |
children | cf0ea082dad2 |
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 |
6260
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
66 static void ogg_update_checksum(AVFormatContext *s, ByteIOContext *pb, int64_t crc_offset) |
2731 | 67 { |
6260
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
68 int64_t pos = url_ftell(pb); |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
69 uint32_t checksum = get_checksum(pb); |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
70 url_fseek(pb, crc_offset, SEEK_SET); |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
71 put_be32(pb, checksum); |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
72 url_fseek(pb, pos, SEEK_SET); |
2731 | 73 } |
74 | |
6260
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
75 static int 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; |
6260
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
78 ByteIOContext *pb; |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3593
diff
changeset
|
79 int64_t crc_offset; |
6260
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
80 int ret, size; |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
81 uint8_t *buf; |
2731 | 82 |
6260
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
83 ret = url_open_dyn_buf(&pb); |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
84 if (ret < 0) |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
85 return ret; |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
86 init_checksum(pb, ff_crc04C11DB7_update, 0); |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
87 put_tag(pb, "OggS"); |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
88 put_byte(pb, 0); |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
89 put_byte(pb, page->flags | extra_flags); |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
90 put_le64(pb, page->granule); |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
91 put_le32(pb, oggstream->serial_num); |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
92 put_le32(pb, oggstream->page_counter++); |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
93 crc_offset = url_ftell(pb); |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
94 put_le32(pb, 0); // crc |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
95 put_byte(pb, page->segments_count); |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
96 put_buffer(pb, page->segments, page->segments_count); |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
97 put_buffer(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
|
98 |
6260
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
99 ogg_update_checksum(s, pb, crc_offset); |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
100 put_flush_packet(pb); |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
101 |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
102 size = url_close_dyn_buf(pb, &buf); |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
103 if (size < 0) |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
104 return size; |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
105 |
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
106 put_buffer(s->pb, buf, size); |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
107 put_flush_packet(s->pb); |
6264
f4f55ad4a603
In ogg muxer, free dyn allocated buffer, fix memleak
bcoudurier
parents:
6260
diff
changeset
|
108 av_free(buf); |
6027
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
109 oggstream->page_count--; |
6260
5c17c20dd67a
In ogg muxer, use dyn buffer to compute crc of the page, fix muxing with pipe
bcoudurier
parents:
6054
diff
changeset
|
110 return 0; |
6027
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
111 } |
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 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
|
114 { |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
115 if (oggstream->kfgshift) |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
116 return (page->granule>>oggstream->kfgshift) + |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
117 (page->granule & ((1<<oggstream->kfgshift)-1)); |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
118 else |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
119 return page->granule; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
120 } |
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 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
|
123 { |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
124 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
|
125 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
|
126 int64_t next_granule, cur_granule; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
127 |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
128 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
|
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 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
|
132 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
|
133 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
|
134 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
|
135 return next_granule > cur_granule; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
136 } |
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 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
|
139 { |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
140 oggstream->page.granule = -1; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
141 oggstream->page.flags = 0; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
142 oggstream->page.segments_count = 0; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
143 oggstream->page.size = 0; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
144 return 0; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
145 } |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
146 |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
147 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
|
148 { |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
149 OGGContext *ogg = s->priv_data; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
150 OGGPageList **p = &ogg->page_list; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
151 OGGPageList *l = av_mallocz(sizeof(*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 if (!l) |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
154 return AVERROR(ENOMEM); |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
155 l->page = oggstream->page; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
156 |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
157 oggstream->page_count++; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
158 ogg_reset_cur_page(oggstream); |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
159 |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
160 while (*p) { |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
161 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
|
162 break; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
163 p = &(*p)->next; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
164 } |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
165 l->next = *p; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
166 *p = l; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
167 |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
168 return 0; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
169 } |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
170 |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
171 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
|
172 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
|
173 { |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
174 OGGStreamContext *oggstream = st->priv_data; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
175 int total_segments = size / 255 + 1; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
176 uint8_t *p = data; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
177 int i, segments, len; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
178 |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
179 for (i = 0; i < total_segments; ) { |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
180 OGGPage *page = &oggstream->page; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
181 |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
182 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
|
183 |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
184 if (i && !page->segments_count) |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
185 page->flags |= 1; // continued packet |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
186 |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
187 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
|
188 page->segments_count += segments - 1; |
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 len = FFMIN(size, segments*255); |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
191 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
|
192 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
|
193 p += len; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
194 size -= len; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
195 i += segments; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
196 page->size += len; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
197 |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
198 if (i == total_segments) |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
199 page->granule = granule; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
200 |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
201 if (page->segments_count == 255) { |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
202 ogg_buffer_page(s, oggstream); |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
203 } |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
204 } |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
205 return 0; |
2731 | 206 } |
207 | |
5280 | 208 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
|
209 int *header_len, AVMetadata *m) |
5280 | 210 { |
211 const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT; | |
212 int size; | |
213 uint8_t *p, *p0; | |
5858
274fed269b59
Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents:
5851
diff
changeset
|
214 unsigned int count; |
5280 | 215 |
5858
274fed269b59
Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents:
5851
diff
changeset
|
216 size = offset + ff_vorbiscomment_length(m, vendor, &count); |
5280 | 217 p = av_mallocz(size); |
218 if (!p) | |
219 return NULL; | |
220 p0 = p; | |
221 | |
222 p += offset; | |
5858
274fed269b59
Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents:
5851
diff
changeset
|
223 ff_vorbiscomment_write(&p, m, vendor, count); |
5280 | 224 |
225 *header_len = size; | |
226 return p0; | |
227 } | |
228 | |
4578 | 229 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
|
230 OGGStreamContext *oggstream, int bitexact, |
274fed269b59
Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents:
5851
diff
changeset
|
231 AVMetadata *m) |
2731 | 232 { |
4578 | 233 enum FLACExtradataFormat format; |
234 uint8_t *streaminfo; | |
2731 | 235 uint8_t *p; |
5054 | 236 |
4578 | 237 if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo)) |
2731 | 238 return -1; |
5054 | 239 |
240 // first packet: STREAMINFO | |
3188 | 241 oggstream->header_len[0] = 51; |
242 oggstream->header[0] = av_mallocz(51); // per ogg flac specs | |
2731 | 243 p = oggstream->header[0]; |
5053 | 244 if (!p) |
5930
08cd1179a20d
Replace all remaining occurrences of AVERROR_NOMEM with
stefano
parents:
5913
diff
changeset
|
245 return AVERROR(ENOMEM); |
2731 | 246 bytestream_put_byte(&p, 0x7F); |
247 bytestream_put_buffer(&p, "FLAC", 4); | |
248 bytestream_put_byte(&p, 1); // major version | |
249 bytestream_put_byte(&p, 0); // minor version | |
250 bytestream_put_be16(&p, 1); // headers packets without this one | |
251 bytestream_put_buffer(&p, "fLaC", 4); | |
252 bytestream_put_byte(&p, 0x00); // streaminfo | |
253 bytestream_put_be24(&p, 34); | |
4578 | 254 bytestream_put_buffer(&p, streaminfo, FLAC_STREAMINFO_SIZE); |
5054 | 255 |
256 // second packet: VorbisComment | |
5858
274fed269b59
Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents:
5851
diff
changeset
|
257 p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1], m); |
5053 | 258 if (!p) |
5930
08cd1179a20d
Replace all remaining occurrences of AVERROR_NOMEM with
stefano
parents:
5913
diff
changeset
|
259 return AVERROR(ENOMEM); |
5280 | 260 oggstream->header[1] = p; |
2731 | 261 bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment |
262 bytestream_put_be24(&p, oggstream->header_len[1] - 4); | |
5280 | 263 |
264 return 0; | |
265 } | |
266 | |
267 #define SPEEX_HEADER_SIZE 80 | |
268 | |
269 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
|
270 OGGStreamContext *oggstream, int bitexact, |
274fed269b59
Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents:
5851
diff
changeset
|
271 AVMetadata *m) |
5280 | 272 { |
273 uint8_t *p; | |
274 | |
275 if (avctx->extradata_size < SPEEX_HEADER_SIZE) | |
276 return -1; | |
277 | |
278 // first packet: Speex header | |
279 p = av_mallocz(SPEEX_HEADER_SIZE); | |
280 if (!p) | |
5930
08cd1179a20d
Replace all remaining occurrences of AVERROR_NOMEM with
stefano
parents:
5913
diff
changeset
|
281 return AVERROR(ENOMEM); |
5280 | 282 oggstream->header[0] = p; |
283 oggstream->header_len[0] = SPEEX_HEADER_SIZE; | |
284 bytestream_put_buffer(&p, avctx->extradata, SPEEX_HEADER_SIZE); | |
285 AV_WL32(&oggstream->header[0][68], 0); // set extra_headers to 0 | |
286 | |
287 // second packet: VorbisComment | |
5858
274fed269b59
Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents:
5851
diff
changeset
|
288 p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1], m); |
5280 | 289 if (!p) |
5930
08cd1179a20d
Replace all remaining occurrences of AVERROR_NOMEM with
stefano
parents:
5913
diff
changeset
|
290 return AVERROR(ENOMEM); |
5280 | 291 oggstream->header[1] = p; |
5054 | 292 |
2731 | 293 return 0; |
294 } | |
295 | |
296 static int ogg_write_header(AVFormatContext *s) | |
297 { | |
298 OGGStreamContext *oggstream; | |
299 int i, j; | |
6054
a198eb01f895
In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents:
6028
diff
changeset
|
300 |
2731 | 301 for (i = 0; i < s->nb_streams; i++) { |
302 AVStream *st = s->streams[i]; | |
6054
a198eb01f895
In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents:
6028
diff
changeset
|
303 unsigned serial_num = i; |
a198eb01f895
In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents:
6028
diff
changeset
|
304 |
5910
536e5527c1e0
Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents:
5858
diff
changeset
|
305 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) |
2731 | 306 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
|
307 else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) |
2731 | 308 av_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den); |
309 if (st->codec->codec_id != CODEC_ID_VORBIS && | |
310 st->codec->codec_id != CODEC_ID_THEORA && | |
5280 | 311 st->codec->codec_id != CODEC_ID_SPEEX && |
2731 | 312 st->codec->codec_id != CODEC_ID_FLAC) { |
313 av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i); | |
314 return -1; | |
315 } | |
316 | |
317 if (!st->codec->extradata || !st->codec->extradata_size) { | |
318 av_log(s, AV_LOG_ERROR, "No extradata present\n"); | |
319 return -1; | |
320 } | |
321 oggstream = av_mallocz(sizeof(*oggstream)); | |
6027
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
322 oggstream->page.stream_index = i; |
6054
a198eb01f895
In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents:
6028
diff
changeset
|
323 |
a198eb01f895
In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents:
6028
diff
changeset
|
324 if (!(st->codec->flags & CODEC_FLAG_BITEXACT)) |
a198eb01f895
In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents:
6028
diff
changeset
|
325 do { |
a198eb01f895
In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents:
6028
diff
changeset
|
326 serial_num = av_get_random_seed(); |
a198eb01f895
In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents:
6028
diff
changeset
|
327 for (j = 0; j < i; j++) { |
a198eb01f895
In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents:
6028
diff
changeset
|
328 OGGStreamContext *sc = s->streams[j]->priv_data; |
a198eb01f895
In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents:
6028
diff
changeset
|
329 if (serial_num == sc->serial_num) |
a198eb01f895
In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents:
6028
diff
changeset
|
330 break; |
a198eb01f895
In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents:
6028
diff
changeset
|
331 } |
a198eb01f895
In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents:
6028
diff
changeset
|
332 } while (j < i); |
a198eb01f895
In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents:
6028
diff
changeset
|
333 oggstream->serial_num = serial_num; |
a198eb01f895
In ogg muxer, use random serial number of each ogg streams
bcoudurier
parents:
6028
diff
changeset
|
334 |
2731 | 335 st->priv_data = oggstream; |
336 if (st->codec->codec_id == CODEC_ID_FLAC) { | |
5055
5a6bbdd352fb
oggenc: return error value from ogg_build_flac_headers()
jbr
parents:
5054
diff
changeset
|
337 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
|
338 st->codec->flags & CODEC_FLAG_BITEXACT, |
274fed269b59
Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents:
5851
diff
changeset
|
339 s->metadata); |
5055
5a6bbdd352fb
oggenc: return error value from ogg_build_flac_headers()
jbr
parents:
5054
diff
changeset
|
340 if (err) { |
5056
f67fad61077e
oggenc: Change error log text. An error here does not necessarily mean
jbr
parents:
5055
diff
changeset
|
341 av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n"); |
2731 | 342 av_freep(&st->priv_data); |
5055
5a6bbdd352fb
oggenc: return error value from ogg_build_flac_headers()
jbr
parents:
5054
diff
changeset
|
343 return err; |
2731 | 344 } |
5280 | 345 } else if (st->codec->codec_id == CODEC_ID_SPEEX) { |
346 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
|
347 st->codec->flags & CODEC_FLAG_BITEXACT, |
274fed269b59
Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents:
5851
diff
changeset
|
348 s->metadata); |
5280 | 349 if (err) { |
350 av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n"); | |
351 av_freep(&st->priv_data); | |
352 return err; | |
353 } | |
2731 | 354 } else { |
355 if (ff_split_xiph_headers(st->codec->extradata, st->codec->extradata_size, | |
356 st->codec->codec_id == CODEC_ID_VORBIS ? 30 : 42, | |
357 oggstream->header, oggstream->header_len) < 0) { | |
358 av_log(s, AV_LOG_ERROR, "Extradata corrupted\n"); | |
359 av_freep(&st->priv_data); | |
360 return -1; | |
361 } | |
362 if (st->codec->codec_id == CODEC_ID_THEORA) { | |
363 /** KFGSHIFT is the width of the less significant section of the granule position | |
364 The less significant section is the frame count since the last keyframe */ | |
365 oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5); | |
366 oggstream->vrev = oggstream->header[0][9]; | |
367 av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n", | |
368 oggstream->kfgshift, oggstream->vrev); | |
369 } | |
370 } | |
371 } | |
6027
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
372 |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
373 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
|
374 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
|
375 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
|
376 oggstream->header_len[0], 0); |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
377 oggstream->page.flags |= 2; // bos |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
378 ogg_buffer_page(s, oggstream); |
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 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
|
381 AVStream *st = s->streams[j]; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
382 OGGStreamContext *oggstream = st->priv_data; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
383 for (i = 1; i < 3; i++) { |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
384 if (oggstream && oggstream->header_len[i]) |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
385 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
|
386 oggstream->header_len[i], 0); |
2731 | 387 } |
6027
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
388 ogg_buffer_page(s, oggstream); |
2731 | 389 } |
390 return 0; | |
391 } | |
392 | |
6027
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
393 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
|
394 { |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
395 OGGContext *ogg = s->priv_data; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
396 OGGPageList *next, *p; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
397 |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
398 if (!ogg->page_list) |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
399 return; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
400 |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
401 for (p = ogg->page_list; p; ) { |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
402 OGGStreamContext *oggstream = |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
403 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
|
404 if (oggstream->page_count < 2 && !flush) |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
405 break; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
406 ogg_write_page(s, &p->page, |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
407 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
|
408 next = p->next; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
409 av_freep(&p); |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
410 p = next; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
411 } |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
412 ogg->page_list = p; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
413 } |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
414 |
2731 | 415 static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt) |
416 { | |
417 AVStream *st = s->streams[pkt->stream_index]; | |
418 OGGStreamContext *oggstream = st->priv_data; | |
6027
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
419 int ret; |
2731 | 420 int64_t granule; |
421 | |
422 if (st->codec->codec_id == CODEC_ID_THEORA) { | |
423 int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration; | |
424 int pframe_count; | |
5913
11bb10c37225
Replace all occurences of PKT_FLAG_KEY with AV_PKT_FLAG_KEY.
cehoyos
parents:
5910
diff
changeset
|
425 if (pkt->flags & AV_PKT_FLAG_KEY) |
2731 | 426 oggstream->last_kf_pts = pts; |
427 pframe_count = pts - oggstream->last_kf_pts; | |
428 // prevent frame count from overflow if key frame flag is not set | |
429 if (pframe_count >= (1<<oggstream->kfgshift)) { | |
430 oggstream->last_kf_pts += pframe_count; | |
431 pframe_count = 0; | |
432 } | |
433 granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count; | |
434 } else | |
435 granule = pkt->pts + pkt->duration; | |
6027
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
436 |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
437 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
|
438 if (ret < 0) |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
439 return ret; |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
440 |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
441 ogg_write_pages(s, 0); |
2731 | 442 |
443 return 0; | |
444 } | |
445 | |
446 static int ogg_write_trailer(AVFormatContext *s) | |
447 { | |
448 int i; | |
6027
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
449 |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
450 /* flush current page */ |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
451 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
|
452 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
|
453 |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
454 ogg_write_pages(s, 1); |
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
455 |
2731 | 456 for (i = 0; i < s->nb_streams; i++) { |
457 AVStream *st = s->streams[i]; | |
458 OGGStreamContext *oggstream = st->priv_data; | |
5280 | 459 if (st->codec->codec_id == CODEC_ID_FLAC || |
460 st->codec->codec_id == CODEC_ID_SPEEX) { | |
2731 | 461 av_free(oggstream->header[0]); |
462 av_free(oggstream->header[1]); | |
463 } | |
464 av_freep(&st->priv_data); | |
465 } | |
466 return 0; | |
467 } | |
468 | |
469 AVOutputFormat ogg_muxer = { | |
470 "ogg", | |
3424
7a0230981402
Make long_names in lavf/lavdev optional depending on CONFIG_SMALL.
diego
parents:
3286
diff
changeset
|
471 NULL_IF_CONFIG_SMALL("Ogg"), |
2731 | 472 "application/ogg", |
5280 | 473 "ogg,ogv,spx", |
6027
710cc5866cbf
In ogg muxer, pack multiple frames into one page, much lower overhead
bcoudurier
parents:
5930
diff
changeset
|
474 sizeof(OGGContext), |
2731 | 475 CODEC_ID_FLAC, |
476 CODEC_ID_THEORA, | |
477 ogg_write_header, | |
478 ogg_write_packet, | |
479 ogg_write_trailer, | |
5858
274fed269b59
Add VorbisComment writing to Ogg/FLAC and Ogg/Speex files.
jbr
parents:
5851
diff
changeset
|
480 .metadata_conv = ff_vorbiscomment_metadata_conv, |
2731 | 481 }; |