Mercurial > libavformat.hg
annotate nutenc.c @ 6073:b9ddee1ca18c libavformat
Make write_header() and write_header() return an error code in case of
error.
author | stefano |
---|---|
date | Wed, 02 Jun 2010 21:57:35 +0000 |
parents | 0475ccdb762d |
children | 258c2ccc68b5 |
rev | line source |
---|---|
2324 | 1 /* |
2 * nut muxer | |
3 * Copyright (c) 2004-2007 Michael Niedermayer | |
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 | |
4201
7d2f3f1b68d8
Fix build: Add intreadwrite.h and bswap.h #includes where necessary.
diego
parents:
4076
diff
changeset
|
22 #include "libavutil/intreadwrite.h" |
3286 | 23 #include "libavutil/tree.h" |
24 #include "libavcodec/mpegaudiodata.h" | |
2324 | 25 #include "nut.h" |
6033 | 26 #include "internal.h" |
3046 | 27 |
28 static int find_expected_header(AVCodecContext *c, int size, int key_frame, uint8_t out[64]){ | |
29 int sample_rate= c->sample_rate; | |
30 | |
31 if(size>4096) | |
32 return 0; | |
33 | |
34 AV_WB24(out, 1); | |
35 | |
36 if(c->codec_id == CODEC_ID_MPEG4){ | |
37 if(key_frame){ | |
38 return 3; | |
39 }else{ | |
40 out[3]= 0xB6; | |
41 return 4; | |
42 } | |
43 }else if(c->codec_id == CODEC_ID_MPEG1VIDEO || c->codec_id == CODEC_ID_MPEG2VIDEO){ | |
44 return 3; | |
45 }else if(c->codec_id == CODEC_ID_H264){ | |
46 return 3; | |
47 }else if(c->codec_id == CODEC_ID_MP3 || c->codec_id == CODEC_ID_MP2){ | |
48 int lsf, mpeg25, sample_rate_index, bitrate_index, frame_size; | |
49 int layer= c->codec_id == CODEC_ID_MP3 ? 3 : 2; | |
50 unsigned int header= 0xFFF00000; | |
51 | |
52 lsf = sample_rate < (24000+32000)/2; | |
53 mpeg25 = sample_rate < (12000+16000)/2; | |
54 sample_rate <<= lsf + mpeg25; | |
55 if (sample_rate < (32000 + 44100)/2) sample_rate_index=2; | |
56 else if(sample_rate < (44100 + 48000)/2) sample_rate_index=0; | |
57 else sample_rate_index=1; | |
58 | |
59 sample_rate= ff_mpa_freq_tab[sample_rate_index] >> (lsf + mpeg25); | |
60 | |
61 for(bitrate_index=2; bitrate_index<30; bitrate_index++){ | |
62 frame_size = ff_mpa_bitrate_tab[lsf][layer-1][bitrate_index>>1]; | |
63 frame_size = (frame_size * 144000) / (sample_rate << lsf) + (bitrate_index&1); | |
64 | |
65 if(frame_size == size) | |
66 break; | |
67 } | |
68 | |
69 header |= (!lsf)<<19; | |
70 header |= (4-layer)<<17; | |
71 header |= 1<<16; //no crc | |
72 AV_WB32(out, header); | |
73 if(size <= 0) | |
3135 | 74 return 2; //we guess there is no crc, if there is one the user clearly does not care about overhead |
3046 | 75 if(bitrate_index == 30) |
76 return -1; //something is wrong ... | |
77 | |
78 header |= (bitrate_index>>1)<<12; | |
79 header |= sample_rate_index<<10; | |
80 header |= (bitrate_index&1)<<9; | |
81 | |
82 return 2; //FIXME actually put the needed ones in build_elision_headers() | |
3135 | 83 return 3; //we guess that the private bit is not set |
84 //FIXME the above assumptions should be checked, if these turn out false too often something should be done | |
3046 | 85 } |
86 return 0; | |
87 } | |
88 | |
89 static int find_header_idx(AVFormatContext *s, AVCodecContext *c, int size, int frame_type){ | |
90 NUTContext *nut = s->priv_data; | |
91 uint8_t out[64]; | |
92 int i; | |
93 int len= find_expected_header(c, size, frame_type, out); | |
94 | |
95 //av_log(NULL, AV_LOG_ERROR, "expected_h len=%d size=%d codec_id=%d\n", len, size, c->codec_id); | |
96 | |
97 for(i=1; i<nut->header_count; i++){ | |
98 if( len == nut->header_len[i] | |
99 && !memcmp(out, nut->header[i], len)){ | |
100 // av_log(NULL, AV_LOG_ERROR, "found %d\n", i); | |
101 return i; | |
102 } | |
103 } | |
104 // av_log(NULL, AV_LOG_ERROR, "nothing found\n"); | |
105 return 0; | |
106 } | |
107 | |
108 static void build_elision_headers(AVFormatContext *s){ | |
109 NUTContext *nut = s->priv_data; | |
110 int i; | |
111 //FIXME this is lame | |
112 //FIXME write a 2pass mode to find the maximal headers | |
4520
c9ef08e6e4a9
cosmetics: 'const static' --> 'static const' to avoid warnings of the type
diego
parents:
4206
diff
changeset
|
113 static const uint8_t headers[][5]={ |
3046 | 114 {3, 0x00, 0x00, 0x01}, |
115 {4, 0x00, 0x00, 0x01, 0xB6}, | |
116 {2, 0xFF, 0xFA}, //mp3+crc | |
117 {2, 0xFF, 0xFB}, //mp3 | |
118 {2, 0xFF, 0xFC}, //mp2+crc | |
119 {2, 0xFF, 0xFD}, //mp2 | |
120 }; | |
121 | |
122 nut->header_count= 7; | |
123 for(i=1; i<nut->header_count; i++){ | |
124 nut->header_len[i]= headers[i-1][0]; | |
125 nut->header [i]= &headers[i-1][1]; | |
126 } | |
127 } | |
2324 | 128 |
129 static void build_frame_code(AVFormatContext *s){ | |
130 NUTContext *nut = s->priv_data; | |
131 int key_frame, index, pred, stream_id; | |
132 int start=1; | |
133 int end= 254; | |
134 int keyframe_0_esc= s->nb_streams > 2; | |
135 int pred_table[10]; | |
2339
2c8579bcd287
add a universal fallback frame code for the case that no other applies
michael
parents:
2332
diff
changeset
|
136 FrameCode *ft; |
2c8579bcd287
add a universal fallback frame code for the case that no other applies
michael
parents:
2332
diff
changeset
|
137 |
2c8579bcd287
add a universal fallback frame code for the case that no other applies
michael
parents:
2332
diff
changeset
|
138 ft= &nut->frame_code[start]; |
2c8579bcd287
add a universal fallback frame code for the case that no other applies
michael
parents:
2332
diff
changeset
|
139 ft->flags= FLAG_CODED; |
2c8579bcd287
add a universal fallback frame code for the case that no other applies
michael
parents:
2332
diff
changeset
|
140 ft->size_mul=1; |
2c8579bcd287
add a universal fallback frame code for the case that no other applies
michael
parents:
2332
diff
changeset
|
141 ft->pts_delta=1; |
2c8579bcd287
add a universal fallback frame code for the case that no other applies
michael
parents:
2332
diff
changeset
|
142 start++; |
2324 | 143 |
144 if(keyframe_0_esc){ | |
145 /* keyframe = 0 escape */ | |
146 FrameCode *ft= &nut->frame_code[start]; | |
147 ft->flags= FLAG_STREAM_ID | FLAG_SIZE_MSB | FLAG_CODED_PTS; | |
148 ft->size_mul=1; | |
149 start++; | |
150 } | |
151 | |
152 for(stream_id= 0; stream_id<s->nb_streams; stream_id++){ | |
153 int start2= start + (end-start)*stream_id / s->nb_streams; | |
154 int end2 = start + (end-start)*(stream_id+1) / s->nb_streams; | |
155 AVCodecContext *codec = s->streams[stream_id]->codec; | |
5910
536e5527c1e0
Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents:
5902
diff
changeset
|
156 int is_audio= codec->codec_type == AVMEDIA_TYPE_AUDIO; |
2324 | 157 int intra_only= /*codec->intra_only || */is_audio; |
158 int pred_count; | |
159 | |
160 for(key_frame=0; key_frame<2; key_frame++){ | |
161 if(intra_only && keyframe_0_esc && key_frame==0) | |
162 continue; | |
163 | |
164 { | |
165 FrameCode *ft= &nut->frame_code[start2]; | |
166 ft->flags= FLAG_KEY*key_frame; | |
167 ft->flags|= FLAG_SIZE_MSB | FLAG_CODED_PTS; | |
168 ft->stream_id= stream_id; | |
169 ft->size_mul=1; | |
3046 | 170 if(is_audio) |
171 ft->header_idx= find_header_idx(s, codec, -1, key_frame); | |
2324 | 172 start2++; |
173 } | |
174 } | |
175 | |
176 key_frame= intra_only; | |
177 #if 1 | |
178 if(is_audio){ | |
179 int frame_bytes= codec->frame_size*(int64_t)codec->bit_rate / (8*codec->sample_rate); | |
180 int pts; | |
181 for(pts=0; pts<2; pts++){ | |
182 for(pred=0; pred<2; pred++){ | |
183 FrameCode *ft= &nut->frame_code[start2]; | |
184 ft->flags= FLAG_KEY*key_frame; | |
185 ft->stream_id= stream_id; | |
186 ft->size_mul=frame_bytes + 2; | |
187 ft->size_lsb=frame_bytes + pred; | |
188 ft->pts_delta=pts; | |
3046 | 189 ft->header_idx= find_header_idx(s, codec, frame_bytes + pred, key_frame); |
2324 | 190 start2++; |
191 } | |
192 } | |
193 }else{ | |
194 FrameCode *ft= &nut->frame_code[start2]; | |
195 ft->flags= FLAG_KEY | FLAG_SIZE_MSB; | |
196 ft->stream_id= stream_id; | |
197 ft->size_mul=1; | |
198 ft->pts_delta=1; | |
199 start2++; | |
200 } | |
201 #endif | |
202 | |
203 if(codec->has_b_frames){ | |
204 pred_count=5; | |
205 pred_table[0]=-2; | |
206 pred_table[1]=-1; | |
207 pred_table[2]=1; | |
208 pred_table[3]=3; | |
209 pred_table[4]=4; | |
210 }else if(codec->codec_id == CODEC_ID_VORBIS){ | |
211 pred_count=3; | |
212 pred_table[0]=2; | |
213 pred_table[1]=9; | |
214 pred_table[2]=16; | |
215 }else{ | |
216 pred_count=1; | |
217 pred_table[0]=1; | |
218 } | |
219 | |
220 for(pred=0; pred<pred_count; pred++){ | |
221 int start3= start2 + (end2-start2)*pred / pred_count; | |
222 int end3 = start2 + (end2-start2)*(pred+1) / pred_count; | |
223 | |
224 for(index=start3; index<end3; index++){ | |
225 FrameCode *ft= &nut->frame_code[index]; | |
226 ft->flags= FLAG_KEY*key_frame; | |
227 ft->flags|= FLAG_SIZE_MSB; | |
228 ft->stream_id= stream_id; | |
229 //FIXME use single byte size and pred from last | |
230 ft->size_mul= end3-start3; | |
231 ft->size_lsb= index - start3; | |
232 ft->pts_delta= pred_table[pred]; | |
3046 | 233 if(is_audio) |
234 ft->header_idx= find_header_idx(s, codec, -1, key_frame); | |
2324 | 235 } |
236 } | |
237 } | |
238 memmove(&nut->frame_code['N'+1], &nut->frame_code['N'], sizeof(FrameCode)*(255-'N')); | |
239 nut->frame_code[ 0].flags= | |
240 nut->frame_code[255].flags= | |
241 nut->frame_code['N'].flags= FLAG_INVALID; | |
242 } | |
243 | |
244 /** | |
245 * Gets the length in bytes which is needed to store val as v. | |
246 */ | |
247 static int get_length(uint64_t val){ | |
248 int i=1; | |
249 | |
250 while(val>>=7) | |
251 i++; | |
252 | |
253 return i; | |
254 } | |
255 | |
256 static void put_v(ByteIOContext *bc, uint64_t val){ | |
257 int i= get_length(val); | |
258 | |
259 while(--i>0) | |
260 put_byte(bc, 128 | (val>>(7*i))); | |
261 | |
262 put_byte(bc, val&127); | |
263 } | |
264 | |
4076 | 265 static void put_tt(NUTContext *nut, StreamContext *nus, ByteIOContext *bc, uint64_t val){ |
2342 | 266 val *= nut->time_base_count; |
267 val += nus->time_base - nut->time_base; | |
268 put_v(bc, val); | |
269 } | |
270 | |
2324 | 271 /** |
2393 | 272 * Stores a string as vb. |
2324 | 273 */ |
274 static void put_str(ByteIOContext *bc, const char *string){ | |
275 int len= strlen(string); | |
276 | |
277 put_v(bc, len); | |
278 put_buffer(bc, string, len); | |
279 } | |
280 | |
281 static void put_s(ByteIOContext *bc, int64_t val){ | |
282 put_v(bc, 2*FFABS(val) - (val>0)); | |
283 } | |
284 | |
285 #ifdef TRACE | |
286 static inline void put_v_trace(ByteIOContext *bc, uint64_t v, char *file, char *func, int line){ | |
2344 | 287 av_log(NULL, AV_LOG_DEBUG, "put_v %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line); |
2324 | 288 |
289 put_v(bc, v); | |
290 } | |
291 | |
292 static inline void put_s_trace(ByteIOContext *bc, int64_t v, char *file, char *func, int line){ | |
2344 | 293 av_log(NULL, AV_LOG_DEBUG, "put_s %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line); |
2324 | 294 |
295 put_s(bc, v); | |
296 } | |
297 #define put_v(bc, v) put_v_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
298 #define put_s(bc, v) put_s_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
299 #endif | |
300 | |
2340 | 301 //FIXME remove calculate_checksum |
2347 | 302 static void put_packet(NUTContext *nut, ByteIOContext *bc, ByteIOContext *dyn_bc, int calculate_checksum, uint64_t startcode){ |
2331
4d1a56112d92
use a dynamic ByteIOContext instead of using the lavf buffer
michael
parents:
2330
diff
changeset
|
303 uint8_t *dyn_buf=NULL; |
4d1a56112d92
use a dynamic ByteIOContext instead of using the lavf buffer
michael
parents:
2330
diff
changeset
|
304 int dyn_size= url_close_dyn_buf(dyn_bc, &dyn_buf); |
2332 | 305 int forw_ptr= dyn_size + 4*calculate_checksum; |
2324 | 306 |
2332 | 307 if(forw_ptr > 4096) |
2683
153d6efc05b8
rename av_crc04C11DB7_update to ff_crc04C11DB7_update and move it to aviobuf.c so it can be reused by other (de)muxers
bcoudurier
parents:
2393
diff
changeset
|
308 init_checksum(bc, ff_crc04C11DB7_update, 0); |
2347 | 309 put_be64(bc, startcode); |
2332 | 310 put_v(bc, forw_ptr); |
311 if(forw_ptr > 4096) | |
312 put_le32(bc, get_checksum(bc)); | |
313 | |
2324 | 314 if(calculate_checksum) |
2683
153d6efc05b8
rename av_crc04C11DB7_update to ff_crc04C11DB7_update and move it to aviobuf.c so it can be reused by other (de)muxers
bcoudurier
parents:
2393
diff
changeset
|
315 init_checksum(bc, ff_crc04C11DB7_update, 0); |
2331
4d1a56112d92
use a dynamic ByteIOContext instead of using the lavf buffer
michael
parents:
2330
diff
changeset
|
316 put_buffer(bc, dyn_buf, dyn_size); |
2324 | 317 if(calculate_checksum) |
2331
4d1a56112d92
use a dynamic ByteIOContext instead of using the lavf buffer
michael
parents:
2330
diff
changeset
|
318 put_le32(bc, get_checksum(bc)); |
2324 | 319 |
2331
4d1a56112d92
use a dynamic ByteIOContext instead of using the lavf buffer
michael
parents:
2330
diff
changeset
|
320 av_free(dyn_buf); |
2324 | 321 } |
322 | |
2329 | 323 static void write_mainheader(NUTContext *nut, ByteIOContext *bc){ |
3046 | 324 int i, j, tmp_pts, tmp_flags, tmp_stream, tmp_mul, tmp_size, tmp_fields, tmp_head_idx; |
325 int64_t tmp_match; | |
2324 | 326 |
2360
b2eeaa4df665
muxer should now be spec compliant (though it likely still contains bugs!)
michael
parents:
2359
diff
changeset
|
327 put_v(bc, 3); /* version */ |
2329 | 328 put_v(bc, nut->avf->nb_streams); |
3014
66fb9d4dea91
nut->max_distance was not set at all anywhere in the NUT muxer
ods15
parents:
3004
diff
changeset
|
329 put_v(bc, nut->max_distance); |
2324 | 330 put_v(bc, nut->time_base_count); |
331 | |
332 for(i=0; i<nut->time_base_count; i++){ | |
333 put_v(bc, nut->time_base[i].num); | |
334 put_v(bc, nut->time_base[i].den); | |
335 } | |
336 | |
337 tmp_pts=0; | |
338 tmp_mul=1; | |
339 tmp_stream=0; | |
3046 | 340 tmp_match= 1-(1LL<<62); |
341 tmp_head_idx= 0; | |
2324 | 342 for(i=0; i<256;){ |
343 tmp_fields=0; | |
344 tmp_size=0; | |
345 // tmp_res=0; | |
346 if(tmp_pts != nut->frame_code[i].pts_delta) tmp_fields=1; | |
347 if(tmp_mul != nut->frame_code[i].size_mul ) tmp_fields=2; | |
348 if(tmp_stream != nut->frame_code[i].stream_id) tmp_fields=3; | |
349 if(tmp_size != nut->frame_code[i].size_lsb ) tmp_fields=4; | |
350 // if(tmp_res != nut->frame_code[i].res ) tmp_fields=5; | |
3046 | 351 if(tmp_head_idx!=nut->frame_code[i].header_idx)tmp_fields=8; |
2324 | 352 |
353 tmp_pts = nut->frame_code[i].pts_delta; | |
354 tmp_flags = nut->frame_code[i].flags; | |
355 tmp_stream= nut->frame_code[i].stream_id; | |
356 tmp_mul = nut->frame_code[i].size_mul; | |
357 tmp_size = nut->frame_code[i].size_lsb; | |
358 // tmp_res = nut->frame_code[i].res; | |
3046 | 359 tmp_head_idx= nut->frame_code[i].header_idx; |
2324 | 360 |
361 for(j=0; i<256; j++,i++){ | |
362 if(i == 'N'){ | |
363 j--; | |
364 continue; | |
365 } | |
366 if(nut->frame_code[i].pts_delta != tmp_pts ) break; | |
367 if(nut->frame_code[i].flags != tmp_flags ) break; | |
368 if(nut->frame_code[i].stream_id != tmp_stream) break; | |
369 if(nut->frame_code[i].size_mul != tmp_mul ) break; | |
370 if(nut->frame_code[i].size_lsb != tmp_size+j) break; | |
371 // if(nut->frame_code[i].res != tmp_res ) break; | |
3046 | 372 if(nut->frame_code[i].header_idx!= tmp_head_idx) break; |
2324 | 373 } |
374 if(j != tmp_mul - tmp_size) tmp_fields=6; | |
375 | |
376 put_v(bc, tmp_flags); | |
377 put_v(bc, tmp_fields); | |
378 if(tmp_fields>0) put_s(bc, tmp_pts); | |
379 if(tmp_fields>1) put_v(bc, tmp_mul); | |
380 if(tmp_fields>2) put_v(bc, tmp_stream); | |
381 if(tmp_fields>3) put_v(bc, tmp_size); | |
382 if(tmp_fields>4) put_v(bc, 0 /*tmp_res*/); | |
383 if(tmp_fields>5) put_v(bc, j); | |
3046 | 384 if(tmp_fields>6) put_v(bc, tmp_match); |
385 if(tmp_fields>7) put_v(bc, tmp_head_idx); | |
386 } | |
387 put_v(bc, nut->header_count-1); | |
388 for(i=1; i<nut->header_count; i++){ | |
389 put_v(bc, nut->header_len[i]); | |
390 put_buffer(bc, nut->header[i], nut->header_len[i]); | |
2324 | 391 } |
2329 | 392 } |
2324 | 393 |
6072
0475ccdb762d
Make write_streamheader() and write_headers() accept an
stefano
parents:
6038
diff
changeset
|
394 static int write_streamheader(AVFormatContext *avctx, ByteIOContext *bc, AVStream *st, int i){ |
0475ccdb762d
Make write_streamheader() and write_headers() accept an
stefano
parents:
6038
diff
changeset
|
395 NUTContext *nut = avctx->priv_data; |
3759
27537074f2a9
convert every muxer/demuxer to write/read sample_aspect_ratio from/to
aurel
parents:
3623
diff
changeset
|
396 AVCodecContext *codec = st->codec; |
2330 | 397 put_v(bc, i); |
398 switch(codec->codec_type){ | |
5910
536e5527c1e0
Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents:
5902
diff
changeset
|
399 case AVMEDIA_TYPE_VIDEO: put_v(bc, 0); break; |
536e5527c1e0
Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents:
5902
diff
changeset
|
400 case AVMEDIA_TYPE_AUDIO: put_v(bc, 1); break; |
536e5527c1e0
Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents:
5902
diff
changeset
|
401 case AVMEDIA_TYPE_SUBTITLE: put_v(bc, 2); break; |
2330 | 402 default : put_v(bc, 3); break; |
403 } | |
404 put_v(bc, 4); | |
405 if (codec->codec_tag){ | |
406 put_le32(bc, codec->codec_tag); | |
407 }else | |
408 return -1; | |
409 | |
2341 | 410 put_v(bc, nut->stream[i].time_base - nut->time_base); |
2330 | 411 put_v(bc, nut->stream[i].msb_pts_shift); |
412 put_v(bc, nut->stream[i].max_pts_distance); | |
413 put_v(bc, codec->has_b_frames); | |
414 put_byte(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */ | |
415 | |
416 put_v(bc, codec->extradata_size); | |
417 put_buffer(bc, codec->extradata, codec->extradata_size); | |
418 | |
419 switch(codec->codec_type){ | |
5910
536e5527c1e0
Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents:
5902
diff
changeset
|
420 case AVMEDIA_TYPE_AUDIO: |
2330 | 421 put_v(bc, codec->sample_rate); |
422 put_v(bc, 1); | |
423 put_v(bc, codec->channels); | |
424 break; | |
5910
536e5527c1e0
Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents:
5902
diff
changeset
|
425 case AVMEDIA_TYPE_VIDEO: |
2330 | 426 put_v(bc, codec->width); |
427 put_v(bc, codec->height); | |
2363 | 428 |
3759
27537074f2a9
convert every muxer/demuxer to write/read sample_aspect_ratio from/to
aurel
parents:
3623
diff
changeset
|
429 if(st->sample_aspect_ratio.num<=0 || st->sample_aspect_ratio.den<=0){ |
2363 | 430 put_v(bc, 0); |
431 put_v(bc, 0); | |
432 }else{ | |
3759
27537074f2a9
convert every muxer/demuxer to write/read sample_aspect_ratio from/to
aurel
parents:
3623
diff
changeset
|
433 put_v(bc, st->sample_aspect_ratio.num); |
27537074f2a9
convert every muxer/demuxer to write/read sample_aspect_ratio from/to
aurel
parents:
3623
diff
changeset
|
434 put_v(bc, st->sample_aspect_ratio.den); |
2363 | 435 } |
2330 | 436 put_v(bc, 0); /* csp type -- unknown */ |
437 break; | |
438 default: | |
439 break; | |
440 } | |
441 return 0; | |
442 } | |
443 | |
3004 | 444 static int add_info(ByteIOContext *bc, const char *type, const char *value){ |
2358 | 445 put_str(bc, type); |
446 put_s(bc, -1); | |
447 put_str(bc, value); | |
448 return 1; | |
449 } | |
450 | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
451 static int write_globalinfo(NUTContext *nut, ByteIOContext *bc){ |
2358 | 452 AVFormatContext *s= nut->avf; |
5704
6b9c2a6d8fa4
Introduce metadata conversion table for NUT muxer and demuxer.
kostya
parents:
5058
diff
changeset
|
453 AVMetadataTag *t = NULL; |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
454 ByteIOContext *dyn_bc; |
2358 | 455 uint8_t *dyn_buf=NULL; |
456 int count=0, dyn_size; | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
457 int ret = url_open_dyn_buf(&dyn_bc); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
458 if(ret < 0) |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
459 return ret; |
2358 | 460 |
5704
6b9c2a6d8fa4
Introduce metadata conversion table for NUT muxer and demuxer.
kostya
parents:
5058
diff
changeset
|
461 while ((t = av_metadata_get(s->metadata, "", t, AV_METADATA_IGNORE_SUFFIX))) |
6b9c2a6d8fa4
Introduce metadata conversion table for NUT muxer and demuxer.
kostya
parents:
5058
diff
changeset
|
462 count += add_info(dyn_bc, t->key, t->value); |
2358 | 463 |
464 put_v(bc, 0); //stream_if_plus1 | |
465 put_v(bc, 0); //chapter_id | |
466 put_v(bc, 0); //timestamp_start | |
467 put_v(bc, 0); //length | |
468 | |
469 put_v(bc, count); | |
470 | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
471 dyn_size= url_close_dyn_buf(dyn_bc, &dyn_buf); |
2358 | 472 put_buffer(bc, dyn_buf, dyn_size); |
473 av_free(dyn_buf); | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
474 return 0; |
2358 | 475 } |
476 | |
3120
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
477 static int write_streaminfo(NUTContext *nut, ByteIOContext *bc, int stream_id){ |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
478 AVFormatContext *s= nut->avf; |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
479 AVStream* st = s->streams[stream_id]; |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
480 ByteIOContext *dyn_bc; |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
481 uint8_t *dyn_buf=NULL; |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
482 int count=0, dyn_size, i; |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
483 int ret = url_open_dyn_buf(&dyn_bc); |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
484 if(ret < 0) |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
485 return ret; |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
486 |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
487 for (i=0; ff_nut_dispositions[i].flag; ++i) { |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
488 if (st->disposition & ff_nut_dispositions[i].flag) |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
489 count += add_info(dyn_bc, "Disposition", ff_nut_dispositions[i].str); |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
490 } |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
491 dyn_size = url_close_dyn_buf(dyn_bc, &dyn_buf); |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
492 |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
493 if (count) { |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
494 put_v(bc, stream_id + 1); //stream_id_plus1 |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
495 put_v(bc, 0); //chapter_id |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
496 put_v(bc, 0); //timestamp_start |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
497 put_v(bc, 0); //length |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
498 |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
499 put_v(bc, count); |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
500 |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
501 put_buffer(bc, dyn_buf, dyn_size); |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
502 } |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
503 |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
504 av_free(dyn_buf); |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
505 return count; |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
506 } |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
507 |
6072
0475ccdb762d
Make write_streamheader() and write_headers() accept an
stefano
parents:
6038
diff
changeset
|
508 static int write_headers(AVFormatContext *avctx, ByteIOContext *bc){ |
0475ccdb762d
Make write_streamheader() and write_headers() accept an
stefano
parents:
6038
diff
changeset
|
509 NUTContext *nut = avctx->priv_data; |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
510 ByteIOContext *dyn_bc; |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
511 int i, ret; |
2345 | 512 |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
513 ret = url_open_dyn_buf(&dyn_bc); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
514 if(ret < 0) |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
515 return ret; |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
516 write_mainheader(nut, dyn_bc); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
517 put_packet(nut, bc, dyn_bc, 1, MAIN_STARTCODE); |
2345 | 518 |
519 for (i=0; i < nut->avf->nb_streams; i++){ | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
520 ret = url_open_dyn_buf(&dyn_bc); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
521 if(ret < 0) |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
522 return ret; |
6073
b9ddee1ca18c
Make write_header() and write_header() return an error code in case of
stefano
parents:
6072
diff
changeset
|
523 if ((ret = write_streamheader(avctx, dyn_bc, nut->avf->streams[i], i)) < 0) |
b9ddee1ca18c
Make write_header() and write_header() return an error code in case of
stefano
parents:
6072
diff
changeset
|
524 return ret; |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
525 put_packet(nut, bc, dyn_bc, 1, STREAM_STARTCODE); |
2345 | 526 } |
2358 | 527 |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
528 ret = url_open_dyn_buf(&dyn_bc); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
529 if(ret < 0) |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
530 return ret; |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
531 write_globalinfo(nut, dyn_bc); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
532 put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE); |
2359 | 533 |
3120
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
534 for (i = 0; i < nut->avf->nb_streams; i++) { |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
535 ret = url_open_dyn_buf(&dyn_bc); |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
536 if(ret < 0) |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
537 return ret; |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
538 ret = write_streaminfo(nut, dyn_bc, i); |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
539 if (ret < 0) |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
540 return ret; |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
541 if (ret > 0) |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
542 put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE); |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
543 else { |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
544 uint8_t* buf; |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
545 url_close_dyn_buf(dyn_bc, &buf); |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
546 av_free(buf); |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
547 } |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
548 } |
ea5623a8efde
Add 'disposition' bitfield to AVStream and use it for both muxing and demuxing
eugeni
parents:
3112
diff
changeset
|
549 |
2359 | 550 nut->last_syncpoint_pos= INT_MIN; |
551 nut->header_count++; | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
552 return 0; |
2345 | 553 } |
554 | |
2329 | 555 static int write_header(AVFormatContext *s){ |
556 NUTContext *nut = s->priv_data; | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
557 ByteIOContext *bc = s->pb; |
6073
b9ddee1ca18c
Make write_header() and write_header() return an error code in case of
stefano
parents:
6072
diff
changeset
|
558 int i, j, ret; |
2329 | 559 |
560 nut->avf= s; | |
561 | |
562 nut->stream = av_mallocz(sizeof(StreamContext)*s->nb_streams); | |
563 nut->time_base= av_mallocz(sizeof(AVRational )*s->nb_streams); | |
564 | |
565 for(i=0; i<s->nb_streams; i++){ | |
566 AVStream *st= s->streams[i]; | |
567 int ssize; | |
568 AVRational time_base; | |
569 ff_parse_specific_params(st->codec, &time_base.den, &ssize, &time_base.num); | |
570 | |
571 av_set_pts_info(st, 64, time_base.num, time_base.den); | |
572 | |
573 for(j=0; j<nut->time_base_count; j++){ | |
574 if(!memcmp(&time_base, &nut->time_base[j], sizeof(AVRational))){ | |
575 break; | |
576 } | |
577 } | |
578 nut->time_base[j]= time_base; | |
579 nut->stream[i].time_base= &nut->time_base[j]; | |
580 if(j==nut->time_base_count) | |
581 nut->time_base_count++; | |
582 | |
583 if(av_q2d(time_base) >= 0.001) | |
584 nut->stream[i].msb_pts_shift = 7; | |
585 else | |
586 nut->stream[i].msb_pts_shift = 14; | |
587 nut->stream[i].max_pts_distance= FFMAX(1/av_q2d(time_base), 1); | |
588 } | |
589 | |
3014
66fb9d4dea91
nut->max_distance was not set at all anywhere in the NUT muxer
ods15
parents:
3004
diff
changeset
|
590 nut->max_distance = MAX_DISTANCE; |
3046 | 591 build_elision_headers(s); |
2329 | 592 build_frame_code(s); |
593 assert(nut->frame_code['N'].flags == FLAG_INVALID); | |
594 | |
595 put_buffer(bc, ID_STRING, strlen(ID_STRING)); | |
596 put_byte(bc, 0); | |
597 | |
6073
b9ddee1ca18c
Make write_header() and write_header() return an error code in case of
stefano
parents:
6072
diff
changeset
|
598 if ((ret = write_headers(s, bc)) < 0) |
b9ddee1ca18c
Make write_header() and write_header() return an error code in case of
stefano
parents:
6072
diff
changeset
|
599 return ret; |
2325 | 600 |
2324 | 601 put_flush_packet(bc); |
602 | |
2359 | 603 //FIXME index |
2324 | 604 |
605 return 0; | |
606 } | |
607 | |
2343 | 608 static int get_needed_flags(NUTContext *nut, StreamContext *nus, FrameCode *fc, AVPacket *pkt){ |
609 int flags= 0; | |
610 | |
5913
11bb10c37225
Replace all occurences of PKT_FLAG_KEY with AV_PKT_FLAG_KEY.
cehoyos
parents:
5910
diff
changeset
|
611 if(pkt->flags & AV_PKT_FLAG_KEY ) flags |= FLAG_KEY; |
2343 | 612 if(pkt->stream_index != fc->stream_id ) flags |= FLAG_STREAM_ID; |
613 if(pkt->size / fc->size_mul ) flags |= FLAG_SIZE_MSB; | |
614 if(pkt->pts - nus->last_pts != fc->pts_delta) flags |= FLAG_CODED_PTS; | |
615 if(pkt->size > 2*nut->max_distance ) flags |= FLAG_CHECKSUM; | |
616 if(FFABS(pkt->pts - nus->last_pts) | |
617 > nus->max_pts_distance) flags |= FLAG_CHECKSUM; | |
3046 | 618 if( pkt->size < nut->header_len[fc->header_idx] |
619 || (pkt->size > 4096 && fc->header_idx) | |
620 || memcmp(pkt->data, nut->header[fc->header_idx], nut->header_len[fc->header_idx])) | |
621 flags |= FLAG_HEADER_IDX; | |
2343 | 622 |
2371 | 623 return flags | (fc->flags & FLAG_CODED); |
2343 | 624 } |
625 | |
3046 | 626 static int find_best_header_idx(NUTContext *nut, AVPacket *pkt){ |
627 int i; | |
628 int best_i = 0; | |
629 int best_len= 0; | |
630 | |
631 if(pkt->size > 4096) | |
632 return 0; | |
633 | |
634 for(i=1; i<nut->header_count; i++){ | |
635 if( pkt->size >= nut->header_len[i] | |
636 && nut->header_len[i] > best_len | |
637 && !memcmp(pkt->data, nut->header[i], nut->header_len[i])){ | |
638 best_i= i; | |
639 best_len= nut->header_len[i]; | |
640 } | |
641 } | |
642 return best_i; | |
643 } | |
644 | |
2324 | 645 static int write_packet(AVFormatContext *s, AVPacket *pkt){ |
2343 | 646 NUTContext *nut = s->priv_data; |
647 StreamContext *nus= &nut->stream[pkt->stream_index]; | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
648 ByteIOContext *bc = s->pb, *dyn_bc; |
2343 | 649 FrameCode *fc; |
650 int64_t coded_pts; | |
3046 | 651 int best_length, frame_code, flags, needed_flags, i, header_idx, best_header_idx; |
5913
11bb10c37225
Replace all occurences of PKT_FLAG_KEY with AV_PKT_FLAG_KEY.
cehoyos
parents:
5910
diff
changeset
|
652 int key_frame = !!(pkt->flags & AV_PKT_FLAG_KEY); |
2343 | 653 int store_sp=0; |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
654 int ret; |
2343 | 655 |
3508 | 656 if(pkt->pts < 0) |
657 return -1; | |
658 | |
2359 | 659 if(1LL<<(20+3*nut->header_count) <= url_ftell(bc)) |
6072
0475ccdb762d
Make write_streamheader() and write_headers() accept an
stefano
parents:
6038
diff
changeset
|
660 write_headers(s, bc); |
2359 | 661 |
3015
db8d14740060
Fix intented algo in syncpoint writing before keyframes in NUT muxer
ods15
parents:
3014
diff
changeset
|
662 if(key_frame && !(nus->last_flags & FLAG_KEY)) |
2343 | 663 store_sp= 1; |
664 | |
665 if(pkt->size + 30/*FIXME check*/ + url_ftell(bc) >= nut->last_syncpoint_pos + nut->max_distance) | |
666 store_sp= 1; | |
667 | |
2393 | 668 //FIXME: Ensure store_sp is 1 in the first place. |
2343 | 669 |
670 if(store_sp){ | |
4076 | 671 Syncpoint *sp, dummy= {.pos= INT64_MAX}; |
2350 | 672 |
2343 | 673 ff_nut_reset_ts(nut, *nus->time_base, pkt->dts); |
2350 | 674 for(i=0; i<s->nb_streams; i++){ |
675 AVStream *st= s->streams[i]; | |
3016
eac2648449a5
back_ptr calculation used wrong timebase when searching for back syncpoint
ods15
parents:
3015
diff
changeset
|
676 int64_t dts_tb = av_rescale_rnd(pkt->dts, |
eac2648449a5
back_ptr calculation used wrong timebase when searching for back syncpoint
ods15
parents:
3015
diff
changeset
|
677 nus->time_base->num * (int64_t)nut->stream[i].time_base->den, |
eac2648449a5
back_ptr calculation used wrong timebase when searching for back syncpoint
ods15
parents:
3015
diff
changeset
|
678 nus->time_base->den * (int64_t)nut->stream[i].time_base->num, |
eac2648449a5
back_ptr calculation used wrong timebase when searching for back syncpoint
ods15
parents:
3015
diff
changeset
|
679 AV_ROUND_DOWN); |
eac2648449a5
back_ptr calculation used wrong timebase when searching for back syncpoint
ods15
parents:
3015
diff
changeset
|
680 int index= av_index_search_timestamp(st, dts_tb, AVSEEK_FLAG_BACKWARD); |
2939 | 681 if(index>=0) dummy.pos= FFMIN(dummy.pos, st->index_entries[index].pos); |
2350 | 682 } |
2939 | 683 if(dummy.pos == INT64_MAX) |
684 dummy.pos= 0; | |
5902 | 685 sp= av_tree_find(nut->syncpoints, &dummy, (void *) ff_nut_sp_pos_cmp, |
686 NULL); | |
2343 | 687 |
688 nut->last_syncpoint_pos= url_ftell(bc); | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
689 ret = url_open_dyn_buf(&dyn_bc); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
690 if(ret < 0) |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
691 return ret; |
4076 | 692 put_tt(nut, nus, dyn_bc, pkt->dts); |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
693 put_v(dyn_bc, sp ? (nut->last_syncpoint_pos - sp->pos)>>4 : 0); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
694 put_packet(nut, bc, dyn_bc, 1, SYNCPOINT_STARTCODE); |
2350 | 695 |
696 ff_nut_add_sp(nut, nut->last_syncpoint_pos, 0/*unused*/, pkt->dts); | |
2343 | 697 } |
698 assert(nus->last_pts != AV_NOPTS_VALUE); | |
699 | |
700 coded_pts = pkt->pts & ((1<<nus->msb_pts_shift)-1); | |
701 if(ff_lsb2full(nus, coded_pts) != pkt->pts) | |
702 coded_pts= pkt->pts + (1<<nus->msb_pts_shift); | |
703 | |
3046 | 704 best_header_idx= find_best_header_idx(nut, pkt); |
705 | |
2343 | 706 best_length=INT_MAX; |
707 frame_code= -1; | |
708 for(i=0; i<256; i++){ | |
709 int length= 0; | |
710 FrameCode *fc= &nut->frame_code[i]; | |
711 int flags= fc->flags; | |
712 | |
713 if(flags & FLAG_INVALID) | |
714 continue; | |
715 needed_flags= get_needed_flags(nut, nus, fc, pkt); | |
716 | |
717 if(flags & FLAG_CODED){ | |
718 length++; | |
2372 | 719 flags = needed_flags; |
2343 | 720 } |
721 | |
722 if((flags & needed_flags) != needed_flags) | |
723 continue; | |
724 | |
725 if((flags ^ needed_flags) & FLAG_KEY) | |
726 continue; | |
727 | |
728 if(flags & FLAG_STREAM_ID) | |
729 length+= get_length(pkt->stream_index); | |
730 | |
731 if(pkt->size % fc->size_mul != fc->size_lsb) | |
732 continue; | |
733 if(flags & FLAG_SIZE_MSB) | |
734 length += get_length(pkt->size / fc->size_mul); | |
735 | |
736 if(flags & FLAG_CHECKSUM) | |
737 length+=4; | |
738 | |
739 if(flags & FLAG_CODED_PTS) | |
740 length += get_length(coded_pts); | |
741 | |
3046 | 742 if( (flags & FLAG_CODED) |
743 && nut->header_len[best_header_idx] > nut->header_len[fc->header_idx]+1){ | |
744 flags |= FLAG_HEADER_IDX; | |
745 } | |
746 | |
747 if(flags & FLAG_HEADER_IDX){ | |
748 length += 1 - nut->header_len[best_header_idx]; | |
749 }else{ | |
750 length -= nut->header_len[fc->header_idx]; | |
751 } | |
752 | |
2343 | 753 length*=4; |
754 length+= !(flags & FLAG_CODED_PTS); | |
755 length+= !(flags & FLAG_CHECKSUM); | |
756 | |
757 if(length < best_length){ | |
758 best_length= length; | |
759 frame_code=i; | |
760 } | |
761 } | |
762 assert(frame_code != -1); | |
763 fc= &nut->frame_code[frame_code]; | |
764 flags= fc->flags; | |
765 needed_flags= get_needed_flags(nut, nus, fc, pkt); | |
3046 | 766 header_idx= fc->header_idx; |
2343 | 767 |
2683
153d6efc05b8
rename av_crc04C11DB7_update to ff_crc04C11DB7_update and move it to aviobuf.c so it can be reused by other (de)muxers
bcoudurier
parents:
2393
diff
changeset
|
768 init_checksum(bc, ff_crc04C11DB7_update, 0); |
2343 | 769 put_byte(bc, frame_code); |
770 if(flags & FLAG_CODED){ | |
771 put_v(bc, (flags^needed_flags) & ~(FLAG_CODED)); | |
772 flags = needed_flags; | |
773 } | |
774 if(flags & FLAG_STREAM_ID) put_v(bc, pkt->stream_index); | |
775 if(flags & FLAG_CODED_PTS) put_v(bc, coded_pts); | |
776 if(flags & FLAG_SIZE_MSB) put_v(bc, pkt->size / fc->size_mul); | |
3046 | 777 if(flags & FLAG_HEADER_IDX) put_v(bc, header_idx= best_header_idx); |
2343 | 778 |
779 if(flags & FLAG_CHECKSUM) put_le32(bc, get_checksum(bc)); | |
780 else get_checksum(bc); | |
781 | |
3046 | 782 put_buffer(bc, pkt->data + nut->header_len[header_idx], pkt->size - nut->header_len[header_idx]); |
2343 | 783 nus->last_flags= flags; |
3047 | 784 nus->last_pts= pkt->pts; |
2350 | 785 |
786 //FIXME just store one per syncpoint | |
787 if(flags & FLAG_KEY) | |
788 av_add_index_entry( | |
789 s->streams[pkt->stream_index], | |
790 nut->last_syncpoint_pos, | |
791 pkt->pts, | |
792 0, | |
793 0, | |
794 AVINDEX_KEYFRAME); | |
795 | |
2324 | 796 return 0; |
797 } | |
798 | |
2346
cbcd840cf971
store headers twice (getting closer to spec compliance ...)
michael
parents:
2345
diff
changeset
|
799 static int write_trailer(AVFormatContext *s){ |
cbcd840cf971
store headers twice (getting closer to spec compliance ...)
michael
parents:
2345
diff
changeset
|
800 NUTContext *nut= s->priv_data; |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2683
diff
changeset
|
801 ByteIOContext *bc= s->pb; |
2346
cbcd840cf971
store headers twice (getting closer to spec compliance ...)
michael
parents:
2345
diff
changeset
|
802 |
2359 | 803 while(nut->header_count<3) |
6072
0475ccdb762d
Make write_streamheader() and write_headers() accept an
stefano
parents:
6038
diff
changeset
|
804 write_headers(s, bc); |
2346
cbcd840cf971
store headers twice (getting closer to spec compliance ...)
michael
parents:
2345
diff
changeset
|
805 put_flush_packet(bc); |
5738 | 806 ff_nut_free_sp(nut); |
5729 | 807 av_freep(&nut->stream); |
808 av_freep(&nut->time_base); | |
2346
cbcd840cf971
store headers twice (getting closer to spec compliance ...)
michael
parents:
2345
diff
changeset
|
809 |
cbcd840cf971
store headers twice (getting closer to spec compliance ...)
michael
parents:
2345
diff
changeset
|
810 return 0; |
cbcd840cf971
store headers twice (getting closer to spec compliance ...)
michael
parents:
2345
diff
changeset
|
811 } |
cbcd840cf971
store headers twice (getting closer to spec compliance ...)
michael
parents:
2345
diff
changeset
|
812 |
2324 | 813 AVOutputFormat nut_muxer = { |
814 "nut", | |
3424
7a0230981402
Make long_names in lavf/lavdev optional depending on CONFIG_SMALL.
diego
parents:
3286
diff
changeset
|
815 NULL_IF_CONFIG_SMALL("NUT format"), |
2324 | 816 "video/x-nut", |
817 "nut", | |
818 sizeof(NUTContext), | |
4206 | 819 #if CONFIG_LIBVORBIS |
2324 | 820 CODEC_ID_VORBIS, |
4206 | 821 #elif CONFIG_LIBMP3LAME |
2324 | 822 CODEC_ID_MP3, |
823 #else | |
3623 | 824 CODEC_ID_MP2, |
2324 | 825 #endif |
826 CODEC_ID_MPEG4, | |
827 write_header, | |
828 write_packet, | |
2346
cbcd840cf971
store headers twice (getting closer to spec compliance ...)
michael
parents:
2345
diff
changeset
|
829 write_trailer, |
4567
963e3b76c7a6
Add AVFMT_VARIABLE_FPS to specify which muxers do not need duplicated frames.
michael
parents:
4520
diff
changeset
|
830 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS, |
6038
d0ea87d82842
Define ff_nut_video_tags and make Nut muxer and demuxer set it in
stefano
parents:
6033
diff
changeset
|
831 .codec_tag = (const AVCodecTag * const []){ ff_codec_bmp_tags, ff_nut_video_tags, ff_codec_wav_tags, ff_nut_subtitle_tags, 0 }, |
5704
6b9c2a6d8fa4
Introduce metadata conversion table for NUT muxer and demuxer.
kostya
parents:
5058
diff
changeset
|
832 .metadata_conv = ff_nut_metadata_conv, |
2324 | 833 }; |