Mercurial > libavformat.hg
annotate nut.c @ 2274:b21c2af60bc9 libavformat
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
author | takis |
---|---|
date | Thu, 19 Jul 2007 15:23:32 +0000 |
parents | 7eb456c4ed8a |
children |
rev | line source |
---|---|
219 | 1 /* |
403 | 2 * "NUT" Container Format muxer and demuxer (DRAFT-200403??) |
219 | 3 * Copyright (c) 2003 Alex Beregszaszi |
403 | 4 * Copyright (c) 2004 Michael Niedermayer |
219 | 5 * |
2215 | 6 * Visit the official site at http://www.nut-container.org/. |
7 * | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1287
diff
changeset
|
8 * This file is part of FFmpeg. |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1287
diff
changeset
|
9 * |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1287
diff
changeset
|
10 * FFmpeg is free software; you can redistribute it and/or |
219 | 11 * modify it under the terms of the GNU Lesser General Public |
12 * License as published by the Free Software Foundation; either | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1287
diff
changeset
|
13 * version 2.1 of the License, or (at your option) any later version. |
219 | 14 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1287
diff
changeset
|
15 * FFmpeg is distributed in the hope that it will be useful, |
219 | 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 * Lesser General Public License for more details. | |
19 * | |
1400 | 20 * You should have received a copy of the GNU Lesser General Public |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1287
diff
changeset
|
21 * License along with FFmpeg; if not, write to the Free Software |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
887
diff
changeset
|
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
219 | 23 */ |
24 | |
25 /* | |
26 * TODO: | |
27 * - index writing | |
415 | 28 * - index packet reading support |
219 | 29 */ |
30 | |
31 //#define DEBUG 1 | |
32 | |
403 | 33 #include <limits.h> |
219 | 34 #include "avformat.h" |
35 #include "mpegaudio.h" | |
1172
6a5e58d2114b
move common stuff from avienc.c and wav.c to new file riff.c
mru
parents:
1169
diff
changeset
|
36 #include "riff.h" |
1176 | 37 #include "adler32.h" |
219 | 38 |
403 | 39 #undef NDEBUG |
40 #include <assert.h> | |
41 | |
461 | 42 //#define TRACE |
43 | |
220 | 44 //from /dev/random |
45 | |
885 | 46 #define MAIN_STARTCODE (0x7A561F5F04ADULL + (((uint64_t)('N'<<8) + 'M')<<48)) |
47 #define STREAM_STARTCODE (0x11405BF2F9DBULL + (((uint64_t)('N'<<8) + 'S')<<48)) | |
48 #define KEYFRAME_STARTCODE (0xE4ADEECA4569ULL + (((uint64_t)('N'<<8) + 'K')<<48)) | |
49 #define INDEX_STARTCODE (0xDD672F23E64EULL + (((uint64_t)('N'<<8) + 'X')<<48)) | |
50 #define INFO_STARTCODE (0xAB68B596BA78ULL + (((uint64_t)('N'<<8) + 'I')<<48)) | |
403 | 51 |
478 | 52 #define ID_STRING "nut/multimedia container\0" |
53 | |
456 | 54 #define MAX_DISTANCE (1024*16-1) |
55 #define MAX_SHORT_DISTANCE (1024*4-1) | |
403 | 56 |
461 | 57 #define FLAG_DATA_SIZE 1 |
58 #define FLAG_KEY_FRAME 2 | |
59 #define FLAG_INVALID 4 | |
403 | 60 |
61 typedef struct { | |
62 uint8_t flags; | |
63 uint8_t stream_id_plus1; | |
456 | 64 uint16_t size_mul; |
65 uint16_t size_lsb; | |
461 | 66 int16_t timestamp_delta; |
67 uint8_t reserved_count; | |
403 | 68 } FrameCode; |
69 | |
70 typedef struct { | |
71 int last_key_frame; | |
72 int msb_timestamp_shift; | |
73 int rate_num; | |
74 int rate_den; | |
75 int64_t last_pts; | |
426 | 76 int64_t last_sync_pos; ///<pos of last 1/2 type frame |
456 | 77 int decode_delay; |
403 | 78 } StreamContext; |
220 | 79 |
219 | 80 typedef struct { |
403 | 81 AVFormatContext *avf; |
82 int written_packet_size; | |
456 | 83 int64_t packet_start[3]; //0-> startcode less, 1-> short startcode 2-> long startcodes |
403 | 84 FrameCode frame_code[256]; |
639 | 85 unsigned int stream_count; |
2164 | 86 uint64_t next_startcode; ///< stores the next startcode if it has already been parsed but the stream is not seekable |
403 | 87 StreamContext *stream; |
456 | 88 int max_distance; |
478 | 89 int max_short_distance; |
461 | 90 int rate_num; |
91 int rate_den; | |
92 int short_startcode; | |
219 | 93 } NUTContext; |
94 | |
415 | 95 static char *info_table[][2]={ |
887 | 96 {NULL , NULL }, // end |
97 {NULL , NULL }, | |
98 {NULL , "UTF8"}, | |
99 {NULL , "v"}, | |
100 {NULL , "s"}, | |
101 {"StreamId" , "v"}, | |
102 {"SegmentId" , "v"}, | |
103 {"StartTimestamp" , "v"}, | |
104 {"EndTimestamp" , "v"}, | |
105 {"Author" , "UTF8"}, | |
106 {"Title" , "UTF8"}, | |
107 {"Description" , "UTF8"}, | |
108 {"Copyright" , "UTF8"}, | |
109 {"Encoder" , "UTF8"}, | |
110 {"Keyword" , "UTF8"}, | |
111 {"Cover" , "JPEG"}, | |
112 {"Cover" , "PNG"}, | |
415 | 113 }; |
114 | |
403 | 115 static void update(NUTContext *nut, int stream_index, int64_t frame_start, int frame_type, int frame_code, int key_frame, int size, int64_t pts){ |
116 StreamContext *stream= &nut->stream[stream_index]; | |
885 | 117 |
403 | 118 stream->last_key_frame= key_frame; |
456 | 119 nut->packet_start[ frame_type ]= frame_start; |
403 | 120 stream->last_pts= pts; |
121 } | |
122 | |
461 | 123 static void reset(AVFormatContext *s, int64_t global_ts){ |
403 | 124 NUTContext *nut = s->priv_data; |
125 int i; | |
885 | 126 |
403 | 127 for(i=0; i<s->nb_streams; i++){ |
128 StreamContext *stream= &nut->stream[i]; | |
885 | 129 |
403 | 130 stream->last_key_frame= 1; |
461 | 131 |
132 stream->last_pts= av_rescale(global_ts, stream->rate_num*(int64_t)nut->rate_den, stream->rate_den*(int64_t)nut->rate_num); | |
403 | 133 } |
134 } | |
135 | |
136 static void build_frame_code(AVFormatContext *s){ | |
137 NUTContext *nut = s->priv_data; | |
461 | 138 int key_frame, index, pred, stream_id; |
403 | 139 int start=0; |
140 int end= 255; | |
141 int keyframe_0_esc= s->nb_streams > 2; | |
461 | 142 int pred_table[10]; |
403 | 143 |
144 if(keyframe_0_esc){ | |
456 | 145 /* keyframe = 0 escape */ |
146 FrameCode *ft= &nut->frame_code[start]; | |
461 | 147 ft->flags= FLAG_DATA_SIZE; |
456 | 148 ft->stream_id_plus1= 0; |
149 ft->size_mul=1; | |
461 | 150 ft->timestamp_delta=0; |
456 | 151 start++; |
403 | 152 } |
153 | |
154 for(stream_id= 0; stream_id<s->nb_streams; stream_id++){ | |
155 int start2= start + (end-start)*stream_id / s->nb_streams; | |
156 int end2 = start + (end-start)*(stream_id+1) / s->nb_streams; | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
157 AVCodecContext *codec = s->streams[stream_id]->codec; |
403 | 158 int is_audio= codec->codec_type == CODEC_TYPE_AUDIO; |
159 int intra_only= /*codec->intra_only || */is_audio; | |
160 int pred_count; | |
161 | |
162 for(key_frame=0; key_frame<2; key_frame++){ | |
163 if(intra_only && keyframe_0_esc && key_frame==0) | |
164 continue; | |
885 | 165 |
456 | 166 { |
167 FrameCode *ft= &nut->frame_code[start2]; | |
168 ft->flags= FLAG_KEY_FRAME*key_frame; | |
461 | 169 ft->flags|= FLAG_DATA_SIZE; |
456 | 170 ft->stream_id_plus1= stream_id + 1; |
171 ft->size_mul=1; | |
461 | 172 ft->timestamp_delta=0; |
456 | 173 start2++; |
403 | 174 } |
175 } | |
176 | |
177 key_frame= intra_only; | |
178 #if 1 | |
179 if(is_audio){ | |
180 int frame_bytes= codec->frame_size*(int64_t)codec->bit_rate / (8*codec->sample_rate); | |
461 | 181 int pts; |
182 for(pts=0; pts<2; pts++){ | |
456 | 183 for(pred=0; pred<2; pred++){ |
403 | 184 FrameCode *ft= &nut->frame_code[start2]; |
461 | 185 ft->flags= FLAG_KEY_FRAME*key_frame; |
403 | 186 ft->stream_id_plus1= stream_id + 1; |
456 | 187 ft->size_mul=frame_bytes + 2; |
188 ft->size_lsb=frame_bytes + pred; | |
461 | 189 ft->timestamp_delta=pts; |
403 | 190 start2++; |
191 } | |
192 } | |
193 }else{ | |
194 FrameCode *ft= &nut->frame_code[start2]; | |
195 ft->flags= FLAG_KEY_FRAME | FLAG_DATA_SIZE; | |
196 ft->stream_id_plus1= stream_id + 1; | |
197 ft->size_mul=1; | |
461 | 198 ft->timestamp_delta=1; |
403 | 199 start2++; |
200 } | |
201 #endif | |
461 | 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 | |
403 | 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]; | |
461 | 226 ft->flags= FLAG_KEY_FRAME*key_frame; |
403 | 227 ft->flags|= FLAG_DATA_SIZE; |
228 ft->stream_id_plus1= stream_id + 1; | |
229 //FIXME use single byte size and pred from last | |
230 ft->size_mul= end3-start3; | |
231 ft->size_lsb= index - start3; | |
461 | 232 ft->timestamp_delta= pred_table[pred]; |
403 | 233 } |
234 } | |
235 } | |
236 memmove(&nut->frame_code['N'+1], &nut->frame_code['N'], sizeof(FrameCode)*(255-'N')); | |
461 | 237 nut->frame_code['N'].flags= FLAG_INVALID; |
403 | 238 } |
239 | |
219 | 240 static uint64_t get_v(ByteIOContext *bc) |
241 { | |
242 uint64_t val = 0; | |
243 | |
421 | 244 for(;;) |
219 | 245 { |
887 | 246 int tmp = get_byte(bc); |
219 | 247 |
887 | 248 if (tmp&0x80) |
249 val= (val<<7) + tmp - 0x80; | |
250 else{ | |
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1400
diff
changeset
|
251 //av_log(NULL, AV_LOG_DEBUG, "get_v()= %"PRId64"\n", (val<<7) + tmp); |
887 | 252 return (val<<7) + tmp; |
456 | 253 } |
219 | 254 } |
255 return -1; | |
256 } | |
257 | |
639 | 258 static int get_str(ByteIOContext *bc, char *string, unsigned int maxlen){ |
259 unsigned int len= get_v(bc); | |
885 | 260 |
415 | 261 if(len && maxlen) |
262 get_buffer(bc, string, FFMIN(len, maxlen)); | |
263 while(len > maxlen){ | |
264 get_byte(bc); | |
265 len--; | |
266 } | |
240 | 267 |
415 | 268 if(maxlen) |
269 string[FFMIN(len, maxlen-1)]= 0; | |
885 | 270 |
415 | 271 if(maxlen == len) |
272 return -1; | |
273 else | |
274 return 0; | |
219 | 275 } |
276 | |
461 | 277 static int64_t get_s(ByteIOContext *bc){ |
278 int64_t v = get_v(bc) + 1; | |
279 | |
280 if (v&1) return -(v>>1); | |
281 else return (v>>1); | |
282 } | |
283 | |
426 | 284 static uint64_t get_vb(ByteIOContext *bc){ |
285 uint64_t val=0; | |
639 | 286 unsigned int i= get_v(bc); |
885 | 287 |
426 | 288 if(i>8) |
289 return UINT64_MAX; | |
885 | 290 |
426 | 291 while(i--) |
292 val = (val<<8) + get_byte(bc); | |
885 | 293 |
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1400
diff
changeset
|
294 //av_log(NULL, AV_LOG_DEBUG, "get_vb()= %"PRId64"\n", val); |
426 | 295 return val; |
296 } | |
297 | |
461 | 298 #ifdef TRACE |
299 static inline uint64_t get_v_trace(ByteIOContext *bc, char *file, char *func, int line){ | |
300 uint64_t v= get_v(bc); | |
301 | |
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1400
diff
changeset
|
302 printf("get_v %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line); |
461 | 303 return v; |
304 } | |
305 | |
306 static inline int64_t get_s_trace(ByteIOContext *bc, char *file, char *func, int line){ | |
307 int64_t v= get_s(bc); | |
308 | |
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1400
diff
changeset
|
309 printf("get_s %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line); |
461 | 310 return v; |
311 } | |
312 | |
313 static inline uint64_t get_vb_trace(ByteIOContext *bc, char *file, char *func, int line){ | |
314 uint64_t v= get_vb(bc); | |
315 | |
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1400
diff
changeset
|
316 printf("get_vb %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line); |
461 | 317 return v; |
318 } | |
319 #define get_v(bc) get_v_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
320 #define get_s(bc) get_s_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
321 #define get_vb(bc) get_vb_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
322 #endif | |
323 | |
324 | |
456 | 325 static int get_packetheader(NUTContext *nut, ByteIOContext *bc, int calculate_checksum) |
219 | 326 { |
456 | 327 int64_t start, size; |
328 start= url_ftell(bc) - 8; | |
403 | 329 |
854 | 330 size= get_v(bc); |
403 | 331 |
1184 | 332 init_checksum(bc, calculate_checksum ? av_adler32_update : NULL, 1); |
403 | 333 |
456 | 334 nut->packet_start[2] = start; |
403 | 335 nut->written_packet_size= size; |
336 | |
337 return size; | |
219 | 338 } |
339 | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
340 static int check_checksum(ByteIOContext *bc){ |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
341 unsigned long checksum= get_checksum(bc); |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
342 return checksum != get_be32(bc); |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
343 } |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
344 |
221 | 345 /** |
885 | 346 * |
221 | 347 */ |
348 static int get_length(uint64_t val){ | |
349 int i; | |
219 | 350 |
426 | 351 for (i=7; val>>i; i+=7); |
219 | 352 |
426 | 353 return i; |
219 | 354 } |
355 | |
419 | 356 static uint64_t find_any_startcode(ByteIOContext *bc, int64_t pos){ |
357 uint64_t state=0; | |
885 | 358 |
419 | 359 if(pos >= 0) |
2164 | 360 url_fseek(bc, pos, SEEK_SET); //note, this may fail if the stream is not seekable, but that should not matter, as in this case we simply start where we are currently |
419 | 361 |
421 | 362 while(!url_feof(bc)){ |
419 | 363 state= (state<<8) | get_byte(bc); |
364 if((state>>56) != 'N') | |
365 continue; | |
366 switch(state){ | |
367 case MAIN_STARTCODE: | |
368 case STREAM_STARTCODE: | |
369 case KEYFRAME_STARTCODE: | |
370 case INFO_STARTCODE: | |
371 case INDEX_STARTCODE: | |
372 return state; | |
373 } | |
374 } | |
426 | 375 |
419 | 376 return 0; |
377 } | |
378 | |
426 | 379 /** |
380 * find the given startcode. | |
381 * @param code the startcode | |
382 * @param pos the start position of the search, or -1 if the current position | |
383 * @returns the position of the startcode or -1 if not found | |
384 */ | |
385 static int64_t find_startcode(ByteIOContext *bc, uint64_t code, int64_t pos){ | |
419 | 386 for(;;){ |
387 uint64_t startcode= find_any_startcode(bc, pos); | |
388 if(startcode == code) | |
426 | 389 return url_ftell(bc) - 8; |
419 | 390 else if(startcode == 0) |
391 return -1; | |
392 pos=-1; | |
393 } | |
394 } | |
395 | |
902 | 396 static int64_t lsb2full(StreamContext *stream, int64_t lsb){ |
397 int64_t mask = (1<<stream->msb_timestamp_shift)-1; | |
398 int64_t delta= stream->last_pts - mask/2; | |
399 return ((lsb - delta)&mask) + delta; | |
400 } | |
401 | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
854
diff
changeset
|
402 #ifdef CONFIG_MUXERS |
461 | 403 |
426 | 404 static void put_v(ByteIOContext *bc, uint64_t val) |
219 | 405 { |
406 int i; | |
407 | |
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1400
diff
changeset
|
408 //av_log(NULL, AV_LOG_DEBUG, "put_v()= %"PRId64"\n", val); |
219 | 409 val &= 0x7FFFFFFFFFFFFFFFULL; // FIXME can only encode upto 63 bits currently |
221 | 410 i= get_length(val); |
219 | 411 |
220 | 412 for (i-=7; i>0; i-=7){ |
887 | 413 put_byte(bc, 0x80 | (val>>i)); |
220 | 414 } |
219 | 415 |
416 put_byte(bc, val&0x7f); | |
417 } | |
418 | |
426 | 419 /** |
420 * stores a string as vb. | |
421 */ | |
422 static void put_str(ByteIOContext *bc, const char *string){ | |
415 | 423 int len= strlen(string); |
885 | 424 |
219 | 425 put_v(bc, len); |
415 | 426 put_buffer(bc, string, len); |
426 | 427 } |
428 | |
699
2f5f4578a076
"put_s should store signed values. Spotted on #mplayerdev by someone I
michael
parents:
683
diff
changeset
|
429 static void put_s(ByteIOContext *bc, int64_t val){ |
461 | 430 if (val<=0) put_v(bc, -2*val ); |
431 else put_v(bc, 2*val-1); | |
432 } | |
433 | |
426 | 434 static void put_vb(ByteIOContext *bc, uint64_t val){ |
435 int i; | |
885 | 436 |
426 | 437 for (i=8; val>>i; i+=8); |
438 | |
439 put_v(bc, i>>3); | |
440 for(i-=8; i>=0; i-=8) | |
441 put_byte(bc, (val>>i)&0xFF); | |
228 | 442 } |
443 | |
461 | 444 #ifdef TRACE |
445 static inline void put_v_trace(ByteIOContext *bc, uint64_t v, char *file, char *func, int line){ | |
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1400
diff
changeset
|
446 printf("get_v %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line); |
885 | 447 |
461 | 448 put_v(bc, v); |
449 } | |
450 | |
451 static inline void put_s_trace(ByteIOContext *bc, int64_t v, char *file, char *func, int line){ | |
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1400
diff
changeset
|
452 printf("get_s %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line); |
885 | 453 |
461 | 454 put_s(bc, v); |
455 } | |
456 | |
457 static inline void put_vb_trace(ByteIOContext *bc, uint64_t v, char *file, char *func, int line){ | |
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1400
diff
changeset
|
458 printf("get_vb %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line); |
885 | 459 |
461 | 460 put_vb(bc, v); |
461 } | |
462 #define put_v(bc, v) put_v_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
463 #define put_s(bc, v) put_s_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
464 #define put_vb(bc, v) put_vb_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
465 #endif | |
466 | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
467 static int put_packetheader(NUTContext *nut, ByteIOContext *bc, int max_size, int calculate_checksum) |
219 | 468 { |
469 put_flush_packet(bc); | |
853 | 470 nut->packet_start[2]= url_ftell(bc) - 8; |
403 | 471 nut->written_packet_size = max_size; |
885 | 472 |
854 | 473 /* packet header */ |
474 put_v(bc, nut->written_packet_size); /* forward ptr */ | |
475 | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
476 if(calculate_checksum) |
1184 | 477 init_checksum(bc, av_adler32_update, 1); |
403 | 478 |
219 | 479 return 0; |
480 } | |
481 | |
853 | 482 /** |
483 * | |
484 * must not be called more then once per packet | |
485 */ | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
486 static int update_packetheader(NUTContext *nut, ByteIOContext *bc, int additional_size, int calculate_checksum){ |
456 | 487 int64_t start= nut->packet_start[2]; |
403 | 488 int64_t cur= url_ftell(bc); |
853 | 489 int size= cur - start - get_length(nut->written_packet_size)/7 - 8; |
885 | 490 |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
491 if(calculate_checksum) |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
492 size += 4; |
885 | 493 |
403 | 494 if(size != nut->written_packet_size){ |
495 int i; | |
496 | |
497 assert( size <= nut->written_packet_size ); | |
885 | 498 |
456 | 499 url_fseek(bc, start + 8, SEEK_SET); |
403 | 500 for(i=get_length(size); i < get_length(nut->written_packet_size); i+=7) |
501 put_byte(bc, 0x80); | |
502 put_v(bc, size); | |
219 | 503 |
403 | 504 url_fseek(bc, cur, SEEK_SET); |
505 nut->written_packet_size= size; //FIXME may fail if multiple updates with differing sizes, as get_length may differ | |
885 | 506 |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
507 if(calculate_checksum) |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
508 put_be32(bc, get_checksum(bc)); |
403 | 509 } |
885 | 510 |
219 | 511 return 0; |
512 } | |
513 | |
514 static int nut_write_header(AVFormatContext *s) | |
515 { | |
516 NUTContext *nut = s->priv_data; | |
517 ByteIOContext *bc = &s->pb; | |
518 AVCodecContext *codec; | |
461 | 519 int i, j, tmp_time, tmp_flags,tmp_stream, tmp_mul, tmp_size, tmp_fields; |
219 | 520 |
1066 | 521 if (strcmp(s->filename, "./data/b-libav.nut")) { |
522 av_log(s, AV_LOG_ERROR, " libavformat NUT is non-compliant and disabled\n"); | |
523 return -1; | |
524 } | |
525 | |
403 | 526 nut->avf= s; |
885 | 527 |
528 nut->stream = | |
887 | 529 av_mallocz(sizeof(StreamContext)*s->nb_streams); |
885 | 530 |
478 | 531 |
532 put_buffer(bc, ID_STRING, strlen(ID_STRING)); | |
533 put_byte(bc, 0); | |
534 nut->packet_start[2]= url_ftell(bc); | |
885 | 535 |
219 | 536 /* main header */ |
220 | 537 put_be64(bc, MAIN_STARTCODE); |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
538 put_packetheader(nut, bc, 120+5*256, 1); |
456 | 539 put_v(bc, 2); /* version */ |
219 | 540 put_v(bc, s->nb_streams); |
456 | 541 put_v(bc, MAX_DISTANCE); |
478 | 542 put_v(bc, MAX_SHORT_DISTANCE); |
885 | 543 |
461 | 544 put_v(bc, nut->rate_num=1); |
545 put_v(bc, nut->rate_den=2); | |
546 put_v(bc, nut->short_startcode=0x4EFE79); | |
885 | 547 |
403 | 548 build_frame_code(s); |
461 | 549 assert(nut->frame_code['N'].flags == FLAG_INVALID); |
885 | 550 |
461 | 551 tmp_time= tmp_flags= tmp_stream= tmp_mul= tmp_size= /*tmp_res=*/ INT_MAX; |
403 | 552 for(i=0; i<256;){ |
461 | 553 tmp_fields=0; |
554 tmp_size= 0; | |
555 if(tmp_time != nut->frame_code[i].timestamp_delta) tmp_fields=1; | |
556 if(tmp_mul != nut->frame_code[i].size_mul ) tmp_fields=2; | |
557 if(tmp_stream != nut->frame_code[i].stream_id_plus1) tmp_fields=3; | |
558 if(tmp_size != nut->frame_code[i].size_lsb ) tmp_fields=4; | |
559 // if(tmp_res != nut->frame_code[i].res ) tmp_fields=5; | |
403 | 560 |
461 | 561 tmp_time = nut->frame_code[i].timestamp_delta; |
562 tmp_flags = nut->frame_code[i].flags; | |
563 tmp_stream= nut->frame_code[i].stream_id_plus1; | |
564 tmp_mul = nut->frame_code[i].size_mul; | |
565 tmp_size = nut->frame_code[i].size_lsb; | |
566 // tmp_res = nut->frame_code[i].res; | |
885 | 567 |
403 | 568 for(j=0; i<256; j++,i++){ |
461 | 569 if(nut->frame_code[i].timestamp_delta != tmp_time ) break; |
403 | 570 if(nut->frame_code[i].flags != tmp_flags ) break; |
571 if(nut->frame_code[i].stream_id_plus1 != tmp_stream) break; | |
572 if(nut->frame_code[i].size_mul != tmp_mul ) break; | |
461 | 573 if(nut->frame_code[i].size_lsb != tmp_size+j) break; |
574 // if(nut->frame_code[i].res != tmp_res ) break; | |
403 | 575 } |
461 | 576 if(j != tmp_mul - tmp_size) tmp_fields=6; |
577 | |
578 put_v(bc, tmp_flags); | |
579 put_v(bc, tmp_fields); | |
580 if(tmp_fields>0) put_s(bc, tmp_time); | |
581 if(tmp_fields>1) put_v(bc, tmp_mul); | |
582 if(tmp_fields>2) put_v(bc, tmp_stream); | |
583 if(tmp_fields>3) put_v(bc, tmp_size); | |
584 if(tmp_fields>4) put_v(bc, 0 /*tmp_res*/); | |
585 if(tmp_fields>5) put_v(bc, j); | |
403 | 586 } |
587 | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
588 update_packetheader(nut, bc, 0, 1); |
885 | 589 |
219 | 590 /* stream headers */ |
591 for (i = 0; i < s->nb_streams; i++) | |
592 { | |
887 | 593 int nom, denom, ssize; |
271 | 594 |
887 | 595 codec = s->streams[i]->codec; |
885 | 596 |
887 | 597 put_be64(bc, STREAM_STARTCODE); |
598 put_packetheader(nut, bc, 120 + codec->extradata_size, 1); | |
599 put_v(bc, i /*s->streams[i]->index*/); | |
782 | 600 switch(codec->codec_type){ |
601 case CODEC_TYPE_VIDEO: put_v(bc, 0); break; | |
602 case CODEC_TYPE_AUDIO: put_v(bc, 1); break; | |
603 // case CODEC_TYPE_TEXT : put_v(bc, 2); break; | |
604 case CODEC_TYPE_DATA : put_v(bc, 3); break; | |
605 default: return -1; | |
606 } | |
887 | 607 if (codec->codec_tag) |
608 put_vb(bc, codec->codec_tag); | |
609 else if (codec->codec_type == CODEC_TYPE_VIDEO) | |
610 { | |
611 put_vb(bc, codec_get_bmp_tag(codec->codec_id)); | |
612 } | |
613 else if (codec->codec_type == CODEC_TYPE_AUDIO) | |
614 { | |
615 put_vb(bc, codec_get_wav_tag(codec->codec_id)); | |
616 } | |
415 | 617 else |
426 | 618 put_vb(bc, 0); |
885 | 619 |
782 | 620 ff_parse_specific_params(codec, &nom, &ssize, &denom); |
282 | 621 |
403 | 622 nut->stream[i].rate_num= nom; |
623 nut->stream[i].rate_den= denom; | |
463
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
624 av_set_pts_info(s->streams[i], 60, denom, nom); |
403 | 625 |
887 | 626 put_v(bc, codec->bit_rate); |
627 put_vb(bc, 0); /* no language code */ | |
628 put_v(bc, nom); | |
629 put_v(bc, denom); | |
403 | 630 if(nom / denom < 1000) |
887 | 631 nut->stream[i].msb_timestamp_shift = 7; |
403 | 632 else |
887 | 633 nut->stream[i].msb_timestamp_shift = 14; |
634 put_v(bc, nut->stream[i].msb_timestamp_shift); | |
456 | 635 put_v(bc, codec->has_b_frames); |
887 | 636 put_byte(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */ |
885 | 637 |
403 | 638 if(codec->extradata_size){ |
639 put_v(bc, 1); | |
640 put_v(bc, codec->extradata_size); | |
885 | 641 put_buffer(bc, codec->extradata, codec->extradata_size); |
403 | 642 } |
887 | 643 put_v(bc, 0); /* end of codec specific headers */ |
885 | 644 |
887 | 645 switch(codec->codec_type) |
646 { | |
647 case CODEC_TYPE_AUDIO: | |
648 put_v(bc, codec->sample_rate); | |
649 put_v(bc, 1); | |
650 put_v(bc, codec->channels); | |
651 break; | |
652 case CODEC_TYPE_VIDEO: | |
653 put_v(bc, codec->width); | |
654 put_v(bc, codec->height); | |
655 put_v(bc, codec->sample_aspect_ratio.num); | |
656 put_v(bc, codec->sample_aspect_ratio.den); | |
657 put_v(bc, 0); /* csp type -- unknown */ | |
658 break; | |
228 | 659 default: |
660 break; | |
887 | 661 } |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
662 update_packetheader(nut, bc, 0, 1); |
219 | 663 } |
664 | |
665 /* info header */ | |
223 | 666 put_be64(bc, INFO_STARTCODE); |
415 | 667 put_packetheader(nut, bc, 30+strlen(s->author)+strlen(s->title)+ |
885 | 668 strlen(s->comment)+strlen(s->copyright)+strlen(LIBAVFORMAT_IDENT), 1); |
219 | 669 if (s->author[0]) |
670 { | |
415 | 671 put_v(bc, 9); /* type */ |
672 put_str(bc, s->author); | |
219 | 673 } |
674 if (s->title[0]) | |
675 { | |
415 | 676 put_v(bc, 10); /* type */ |
677 put_str(bc, s->title); | |
219 | 678 } |
679 if (s->comment[0]) | |
680 { | |
415 | 681 put_v(bc, 11); /* type */ |
682 put_str(bc, s->comment); | |
219 | 683 } |
684 if (s->copyright[0]) | |
685 { | |
415 | 686 put_v(bc, 12); /* type */ |
687 put_str(bc, s->copyright); | |
219 | 688 } |
689 /* encoder */ | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
690 if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)){ |
441 | 691 put_v(bc, 13); /* type */ |
692 put_str(bc, LIBAVFORMAT_IDENT); | |
693 } | |
885 | 694 |
222 | 695 put_v(bc, 0); /* eof info */ |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
696 update_packetheader(nut, bc, 0, 1); |
885 | 697 |
219 | 698 put_flush_packet(bc); |
885 | 699 |
219 | 700 return 0; |
701 } | |
702 | |
468 | 703 static int nut_write_packet(AVFormatContext *s, AVPacket *pkt) |
219 | 704 { |
705 NUTContext *nut = s->priv_data; | |
468 | 706 StreamContext *stream= &nut->stream[pkt->stream_index]; |
219 | 707 ByteIOContext *bc = &s->pb; |
403 | 708 int key_frame = 0, full_pts=0; |
219 | 709 AVCodecContext *enc; |
456 | 710 int64_t coded_pts; |
461 | 711 int frame_type, best_length, frame_code, flags, i, size_mul, size_lsb, time_delta; |
403 | 712 const int64_t frame_start= url_ftell(bc); |
468 | 713 int64_t pts= pkt->pts; |
714 int size= pkt->size; | |
715 int stream_index= pkt->stream_index; | |
219 | 716 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
717 enc = s->streams[stream_index]->codec; |
468 | 718 key_frame = !!(pkt->flags & PKT_FLAG_KEY); |
885 | 719 |
403 | 720 frame_type=0; |
456 | 721 if(frame_start + size + 20 - FFMAX(nut->packet_start[1], nut->packet_start[2]) > MAX_DISTANCE) |
722 frame_type=2; | |
723 if(key_frame && !stream->last_key_frame) | |
724 frame_type=2; | |
725 | |
461 | 726 if(frame_type>1){ |
727 int64_t global_ts= av_rescale(pts, stream->rate_den*(int64_t)nut->rate_num, stream->rate_num*(int64_t)nut->rate_den); | |
728 reset(s, global_ts); | |
887 | 729 put_be64(bc, KEYFRAME_STARTCODE); |
461 | 730 put_v(bc, global_ts); |
731 } | |
732 assert(stream->last_pts != AV_NOPTS_VALUE); | |
733 coded_pts = pts & ((1<<stream->msb_timestamp_shift)-1); | |
734 if(lsb2full(stream, coded_pts) != pts) | |
456 | 735 full_pts=1; |
328 | 736 |
456 | 737 if(full_pts) |
738 coded_pts= pts + (1<<stream->msb_timestamp_shift); | |
403 | 739 |
740 best_length=INT_MAX; | |
741 frame_code= -1; | |
742 for(i=0; i<256; i++){ | |
743 int stream_id_plus1= nut->frame_code[i].stream_id_plus1; | |
429
983639863758
removing keyframe prediction and checksum threshold
michael
parents:
427
diff
changeset
|
744 int fc_key_frame; |
403 | 745 int length=0; |
746 size_mul= nut->frame_code[i].size_mul; | |
747 size_lsb= nut->frame_code[i].size_lsb; | |
461 | 748 time_delta= nut->frame_code[i].timestamp_delta; |
403 | 749 flags= nut->frame_code[i].flags; |
750 | |
456 | 751 assert(size_mul > size_lsb); |
885 | 752 |
403 | 753 if(stream_id_plus1 == 0) length+= get_length(stream_index); |
754 else if(stream_id_plus1 - 1 != stream_index) | |
755 continue; | |
429
983639863758
removing keyframe prediction and checksum threshold
michael
parents:
427
diff
changeset
|
756 fc_key_frame= !!(flags & FLAG_KEY_FRAME); |
983639863758
removing keyframe prediction and checksum threshold
michael
parents:
427
diff
changeset
|
757 |
403 | 758 assert(key_frame==0 || key_frame==1); |
759 if(fc_key_frame != key_frame) | |
760 continue; | |
761 | |
456 | 762 if(flags & FLAG_DATA_SIZE){ |
403 | 763 if(size % size_mul != size_lsb) |
764 continue; | |
456 | 765 length += get_length(size / size_mul); |
766 }else if(size != size_lsb) | |
767 continue; | |
220 | 768 |
461 | 769 if(full_pts && time_delta) |
403 | 770 continue; |
885 | 771 |
461 | 772 if(!time_delta){ |
456 | 773 length += get_length(coded_pts); |
403 | 774 }else{ |
461 | 775 if(time_delta != pts - stream->last_pts) |
403 | 776 continue; |
777 } | |
778 | |
779 if(length < best_length){ | |
780 best_length= length; | |
781 frame_code=i; | |
782 } | |
783 // av_log(s, AV_LOG_DEBUG, "%d %d %d %d %d %d %d %d %d %d\n", key_frame, frame_type, full_pts, size, stream_index, flags, size_mul, size_lsb, stream_id_plus1, length); | |
784 } | |
223 | 785 |
403 | 786 assert(frame_code != -1); |
787 flags= nut->frame_code[frame_code].flags; | |
788 size_mul= nut->frame_code[frame_code].size_mul; | |
789 size_lsb= nut->frame_code[frame_code].size_lsb; | |
461 | 790 time_delta= nut->frame_code[frame_code].timestamp_delta; |
791 #ifdef TRACE | |
403 | 792 best_length /= 7; |
793 best_length ++; //frame_code | |
456 | 794 if(frame_type==2){ |
795 best_length += 8; // startcode | |
403 | 796 } |
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1400
diff
changeset
|
797 av_log(s, AV_LOG_DEBUG, "kf:%d ft:%d pt:%d fc:%2X len:%2d size:%d stream:%d flag:%d mul:%d lsb:%d s+1:%d pts_delta:%d pts:%"PRId64" fs:%"PRId64"\n", key_frame, frame_type, full_pts ? 1 : 0, frame_code, best_length, size, stream_index, flags, size_mul, size_lsb, nut->frame_code[frame_code].stream_id_plus1,(int)(pts - stream->last_pts), pts, frame_start); |
456 | 798 // av_log(s, AV_LOG_DEBUG, "%d %d %d\n", stream->lru_pts_delta[0], stream->lru_pts_delta[1], stream->lru_pts_delta[2]); |
403 | 799 #endif |
800 | |
456 | 801 assert(frame_type != 1); //short startcode not implemented yet |
403 | 802 put_byte(bc, frame_code); |
803 | |
804 if(nut->frame_code[frame_code].stream_id_plus1 == 0) | |
805 put_v(bc, stream_index); | |
461 | 806 if (!time_delta){ |
456 | 807 put_v(bc, coded_pts); |
403 | 808 } |
809 if(flags & FLAG_DATA_SIZE) | |
810 put_v(bc, size / size_mul); | |
461 | 811 else |
812 assert(size == size_lsb); | |
456 | 813 if(size > MAX_DISTANCE){ |
814 assert(frame_type > 1); | |
403 | 815 } |
885 | 816 |
468 | 817 put_buffer(bc, pkt->data, size); |
403 | 818 |
819 update(nut, stream_index, frame_start, frame_type, frame_code, key_frame, size, pts); | |
885 | 820 |
219 | 821 return 0; |
822 } | |
823 | |
824 static int nut_write_trailer(AVFormatContext *s) | |
825 { | |
328 | 826 NUTContext *nut = s->priv_data; |
219 | 827 ByteIOContext *bc = &s->pb; |
403 | 828 |
219 | 829 #if 0 |
830 int i; | |
831 | |
832 /* WRITE INDEX */ | |
833 | |
834 for (i = 0; s->nb_streams; i++) | |
835 { | |
887 | 836 put_be64(bc, INDEX_STARTCODE); |
837 put_packetheader(nut, bc, 64, 1); | |
838 put_v(bc, s->streams[i]->id); | |
839 put_v(bc, ...); | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
840 update_packetheader(nut, bc, 0, 1); |
219 | 841 } |
842 #endif | |
843 | |
844 put_flush_packet(bc); | |
885 | 845 |
403 | 846 av_freep(&nut->stream); |
219 | 847 |
848 return 0; | |
849 } | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
854
diff
changeset
|
850 #endif //CONFIG_MUXERS |
219 | 851 |
852 static int nut_probe(AVProbeData *p) | |
853 { | |
220 | 854 int i; |
456 | 855 uint64_t code= 0xff; |
220 | 856 |
857 for (i = 0; i < p->buf_size; i++) { | |
456 | 858 code = (code << 8) | p->buf[i]; |
220 | 859 if (code == MAIN_STARTCODE) |
860 return AVPROBE_SCORE_MAX; | |
861 } | |
862 return 0; | |
219 | 863 } |
864 | |
419 | 865 static int decode_main_header(NUTContext *nut){ |
866 AVFormatContext *s= nut->avf; | |
219 | 867 ByteIOContext *bc = &s->pb; |
220 | 868 uint64_t tmp; |
461 | 869 int i, j, tmp_stream, tmp_mul, tmp_time, tmp_size, count, tmp_res; |
885 | 870 |
456 | 871 get_packetheader(nut, bc, 1); |
403 | 872 |
419 | 873 tmp = get_v(bc); |
456 | 874 if (tmp != 2){ |
887 | 875 av_log(s, AV_LOG_ERROR, "bad version (%"PRId64")\n", tmp); |
419 | 876 return -1; |
877 } | |
885 | 878 |
419 | 879 nut->stream_count = get_v(bc); |
639 | 880 if(nut->stream_count > MAX_STREAMS){ |
881 av_log(s, AV_LOG_ERROR, "too many streams\n"); | |
882 return -1; | |
883 } | |
456 | 884 nut->max_distance = get_v(bc); |
478 | 885 nut->max_short_distance = get_v(bc); |
461 | 886 nut->rate_num= get_v(bc); |
887 nut->rate_den= get_v(bc); | |
888 nut->short_startcode= get_v(bc); | |
889 if(nut->short_startcode>>16 != 'N'){ | |
887 | 890 av_log(s, AV_LOG_ERROR, "invalid short startcode %X\n", nut->short_startcode); |
461 | 891 return -1; |
892 } | |
885 | 893 |
403 | 894 for(i=0; i<256;){ |
895 int tmp_flags = get_v(bc); | |
461 | 896 int tmp_fields= get_v(bc); |
897 if(tmp_fields>0) tmp_time = get_s(bc); | |
898 if(tmp_fields>1) tmp_mul = get_v(bc); | |
899 if(tmp_fields>2) tmp_stream= get_v(bc); | |
900 if(tmp_fields>3) tmp_size = get_v(bc); | |
901 else tmp_size = 0; | |
902 if(tmp_fields>4) tmp_res = get_v(bc); | |
903 else tmp_res = 0; | |
904 if(tmp_fields>5) count = get_v(bc); | |
905 else count = tmp_mul - tmp_size; | |
885 | 906 |
907 while(tmp_fields-- > 6) | |
461 | 908 get_v(bc); |
885 | 909 |
403 | 910 if(count == 0 || i+count > 256){ |
911 av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i); | |
912 return -1; | |
913 } | |
461 | 914 if(tmp_stream > nut->stream_count + 1){ |
915 av_log(s, AV_LOG_ERROR, "illegal stream number\n"); | |
916 return -1; | |
917 } | |
403 | 918 |
919 for(j=0; j<count; j++,i++){ | |
920 nut->frame_code[i].flags = tmp_flags ; | |
461 | 921 nut->frame_code[i].timestamp_delta = tmp_time ; |
403 | 922 nut->frame_code[i].stream_id_plus1 = tmp_stream; |
923 nut->frame_code[i].size_mul = tmp_mul ; | |
461 | 924 nut->frame_code[i].size_lsb = tmp_size+j; |
925 nut->frame_code[i].reserved_count = tmp_res ; | |
403 | 926 } |
927 } | |
461 | 928 if(nut->frame_code['N'].flags != FLAG_INVALID){ |
403 | 929 av_log(s, AV_LOG_ERROR, "illegal frame_code table\n"); |
930 return -1; | |
931 } | |
419 | 932 |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
933 if(check_checksum(bc)){ |
735
72f8690c3f37
Ministry of English Composition, reporting for duty (and the word is "skipped", not "skiped"; "skiped" would rhyme with "hyped")
melanson
parents:
709
diff
changeset
|
934 av_log(s, AV_LOG_ERROR, "Main header checksum mismatch\n"); |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
935 return -1; |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
936 } |
419 | 937 |
938 return 0; | |
939 } | |
940 | |
941 static int decode_stream_header(NUTContext *nut){ | |
942 AVFormatContext *s= nut->avf; | |
943 ByteIOContext *bc = &s->pb; | |
461 | 944 int class, nom, denom, stream_id; |
419 | 945 uint64_t tmp; |
946 AVStream *st; | |
885 | 947 |
456 | 948 get_packetheader(nut, bc, 1); |
419 | 949 stream_id= get_v(bc); |
950 if(stream_id >= nut->stream_count || s->streams[stream_id]) | |
951 return -1; | |
885 | 952 |
419 | 953 st = av_new_stream(s, stream_id); |
954 if (!st) | |
2273
7eb456c4ed8a
Replace all occurrences of AVERROR_NOMEM with AVERROR(ENOMEM).
takis
parents:
2215
diff
changeset
|
955 return AVERROR(ENOMEM); |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
461
diff
changeset
|
956 |
419 | 957 class = get_v(bc); |
426 | 958 tmp = get_vb(bc); |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
959 st->codec->codec_tag= tmp; |
419 | 960 switch(class) |
961 { | |
962 case 0: | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
963 st->codec->codec_type = CODEC_TYPE_VIDEO; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
964 st->codec->codec_id = codec_get_bmp_id(tmp); |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
965 if (st->codec->codec_id == CODEC_ID_NONE) |
419 | 966 av_log(s, AV_LOG_ERROR, "Unknown codec?!\n"); |
967 break; | |
782 | 968 case 1: |
969 case 32: //compatibility | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
970 st->codec->codec_type = CODEC_TYPE_AUDIO; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
971 st->codec->codec_id = codec_get_wav_id(tmp); |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
972 if (st->codec->codec_id == CODEC_ID_NONE) |
419 | 973 av_log(s, AV_LOG_ERROR, "Unknown codec?!\n"); |
974 break; | |
782 | 975 case 2: |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
976 // st->codec->codec_type = CODEC_TYPE_TEXT; |
782 | 977 // break; |
978 case 3: | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
979 st->codec->codec_type = CODEC_TYPE_DATA; |
782 | 980 break; |
419 | 981 default: |
982 av_log(s, AV_LOG_ERROR, "Unknown stream class (%d)\n", class); | |
983 return -1; | |
984 } | |
985 s->bit_rate += get_v(bc); | |
426 | 986 get_vb(bc); /* language code */ |
419 | 987 nom = get_v(bc); |
988 denom = get_v(bc); | |
989 nut->stream[stream_id].msb_timestamp_shift = get_v(bc); | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
990 st->codec->has_b_frames= |
456 | 991 nut->stream[stream_id].decode_delay= get_v(bc); |
419 | 992 get_byte(bc); /* flags */ |
993 | |
994 /* codec specific data headers */ | |
995 while(get_v(bc) != 0){ | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
996 st->codec->extradata_size= get_v(bc); |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
997 if((unsigned)st->codec->extradata_size > (1<<30)) |
639 | 998 return -1; |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
999 st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); |
885 | 1000 get_buffer(bc, st->codec->extradata, st->codec->extradata_size); |
887 | 1001 // url_fskip(bc, get_v(bc)); |
419 | 1002 } |
885 | 1003 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
1004 if (st->codec->codec_type == CODEC_TYPE_VIDEO) /* VIDEO */ |
419 | 1005 { |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
1006 st->codec->width = get_v(bc); |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
1007 st->codec->height = get_v(bc); |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
1008 st->codec->sample_aspect_ratio.num= get_v(bc); |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
1009 st->codec->sample_aspect_ratio.den= get_v(bc); |
419 | 1010 get_v(bc); /* csp type */ |
1011 } | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
1012 if (st->codec->codec_type == CODEC_TYPE_AUDIO) /* AUDIO */ |
419 | 1013 { |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
1014 st->codec->sample_rate = get_v(bc); |
456 | 1015 get_v(bc); // samplerate_den |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
798
diff
changeset
|
1016 st->codec->channels = get_v(bc); |
419 | 1017 } |
1018 if(check_checksum(bc)){ | |
735
72f8690c3f37
Ministry of English Composition, reporting for duty (and the word is "skipped", not "skiped"; "skiped" would rhyme with "hyped")
melanson
parents:
709
diff
changeset
|
1019 av_log(s, AV_LOG_ERROR, "Stream header %d checksum mismatch\n", stream_id); |
419 | 1020 return -1; |
1021 } | |
463
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1022 av_set_pts_info(s->streams[stream_id], 60, denom, nom); |
419 | 1023 nut->stream[stream_id].rate_num= nom; |
1024 nut->stream[stream_id].rate_den= denom; | |
1025 return 0; | |
1026 } | |
1027 | |
1028 static int decode_info_header(NUTContext *nut){ | |
1029 AVFormatContext *s= nut->avf; | |
1030 ByteIOContext *bc = &s->pb; | |
885 | 1031 |
456 | 1032 get_packetheader(nut, bc, 1); |
419 | 1033 |
1034 for(;;){ | |
1035 int id= get_v(bc); | |
1036 char *name, *type, custom_name[256], custom_type[256]; | |
1037 | |
1038 if(!id) | |
1039 break; | |
1040 else if(id >= sizeof(info_table)/sizeof(info_table[0])){ | |
881 | 1041 av_log(s, AV_LOG_ERROR, "info id is too large %d %zd\n", id, sizeof(info_table)/sizeof(info_table[0])); |
419 | 1042 return -1; |
1043 } | |
1044 | |
1045 type= info_table[id][1]; | |
1046 name= info_table[id][0]; | |
1047 //av_log(s, AV_LOG_DEBUG, "%d %s %s\n", id, type, name); | |
1048 | |
1049 if(!type){ | |
1050 get_str(bc, custom_type, sizeof(custom_type)); | |
1051 type= custom_type; | |
1052 } | |
1053 if(!name){ | |
1054 get_str(bc, custom_name, sizeof(custom_name)); | |
1055 name= custom_name; | |
1056 } | |
885 | 1057 |
419 | 1058 if(!strcmp(type, "v")){ |
683
095009fc2f35
kill warnings patch by (M«©ns Rullg«©rd <mru inprovide com>)
michael
parents:
682
diff
changeset
|
1059 get_v(bc); |
419 | 1060 }else{ |
1061 if(!strcmp(name, "Author")) | |
1062 get_str(bc, s->author, sizeof(s->author)); | |
1063 else if(!strcmp(name, "Title")) | |
1064 get_str(bc, s->title, sizeof(s->title)); | |
1065 else if(!strcmp(name, "Copyright")) | |
1066 get_str(bc, s->copyright, sizeof(s->copyright)); | |
1067 else if(!strcmp(name, "Description")) | |
1068 get_str(bc, s->comment, sizeof(s->comment)); | |
1069 else | |
1070 get_str(bc, NULL, 0); | |
1071 } | |
1072 } | |
1073 if(check_checksum(bc)){ | |
735
72f8690c3f37
Ministry of English Composition, reporting for duty (and the word is "skipped", not "skiped"; "skiped" would rhyme with "hyped")
melanson
parents:
709
diff
changeset
|
1074 av_log(s, AV_LOG_ERROR, "Info header checksum mismatch\n"); |
419 | 1075 return -1; |
1076 } | |
1077 return 0; | |
1078 } | |
1079 | |
1080 static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap) | |
1081 { | |
1082 NUTContext *nut = s->priv_data; | |
1083 ByteIOContext *bc = &s->pb; | |
1084 int64_t pos; | |
1085 int inited_stream_count; | |
1086 | |
1087 nut->avf= s; | |
885 | 1088 |
419 | 1089 /* main header */ |
1090 pos=0; | |
1091 for(;;){ | |
426 | 1092 pos= find_startcode(bc, MAIN_STARTCODE, pos)+1; |
1093 if (pos<0){ | |
419 | 1094 av_log(s, AV_LOG_ERROR, "no main startcode found\n"); |
1095 return -1; | |
1096 } | |
1097 if(decode_main_header(nut) >= 0) | |
1098 break; | |
1099 } | |
885 | 1100 |
1101 | |
219 | 1102 s->bit_rate = 0; |
328 | 1103 |
419 | 1104 nut->stream = av_malloc(sizeof(StreamContext)*nut->stream_count); |
272 | 1105 |
419 | 1106 /* stream headers */ |
1107 pos=0; | |
1108 for(inited_stream_count=0; inited_stream_count < nut->stream_count;){ | |
426 | 1109 pos= find_startcode(bc, STREAM_STARTCODE, pos)+1; |
782 | 1110 if (pos<0+1){ |
419 | 1111 av_log(s, AV_LOG_ERROR, "not all stream headers found\n"); |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
1112 return -1; |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
1113 } |
419 | 1114 if(decode_stream_header(nut) >= 0) |
1115 inited_stream_count++; | |
415 | 1116 } |
1117 | |
419 | 1118 /* info headers */ |
1119 pos=0; | |
1120 for(;;){ | |
1121 uint64_t startcode= find_any_startcode(bc, pos); | |
1122 pos= url_ftell(bc); | |
415 | 1123 |
419 | 1124 if(startcode==0){ |
1125 av_log(s, AV_LOG_ERROR, "EOF before video frames\n"); | |
1126 return -1; | |
1127 }else if(startcode == KEYFRAME_STARTCODE){ | |
421 | 1128 nut->next_startcode= startcode; |
419 | 1129 break; |
1130 }else if(startcode != INFO_STARTCODE){ | |
1131 continue; | |
415 | 1132 } |
419 | 1133 |
1134 decode_info_header(nut); | |
1135 } | |
1136 | |
219 | 1137 return 0; |
1138 } | |
1139 | |
461 | 1140 static int decode_frame_header(NUTContext *nut, int *key_frame_ret, int64_t *pts_ret, int *stream_id_ret, int frame_code, int frame_type, int64_t frame_start){ |
421 | 1141 AVFormatContext *s= nut->avf; |
403 | 1142 StreamContext *stream; |
219 | 1143 ByteIOContext *bc = &s->pb; |
461 | 1144 int size, flags, size_mul, size_lsb, stream_id, time_delta; |
328 | 1145 int64_t pts = 0; |
219 | 1146 |
456 | 1147 if(frame_type < 2 && frame_start - nut->packet_start[2] > nut->max_distance){ |
1148 av_log(s, AV_LOG_ERROR, "last frame must have been damaged\n"); | |
1149 return -1; | |
1150 } | |
1151 | |
1152 if(frame_type) | |
1153 nut->packet_start[ frame_type ]= frame_start; //otherwise 1 goto 1 may happen | |
885 | 1154 |
403 | 1155 flags= nut->frame_code[frame_code].flags; |
1156 size_mul= nut->frame_code[frame_code].size_mul; | |
1157 size_lsb= nut->frame_code[frame_code].size_lsb; | |
1158 stream_id= nut->frame_code[frame_code].stream_id_plus1 - 1; | |
461 | 1159 time_delta= nut->frame_code[frame_code].timestamp_delta; |
885 | 1160 |
403 | 1161 if(stream_id==-1) |
1162 stream_id= get_v(bc); | |
1163 if(stream_id >= s->nb_streams){ | |
1164 av_log(s, AV_LOG_ERROR, "illegal stream_id\n"); | |
1165 return -1; | |
1166 } | |
1167 stream= &nut->stream[stream_id]; | |
421 | 1168 |
1169 // av_log(s, AV_LOG_DEBUG, "ft:%d ppts:%d %d %d\n", frame_type, stream->lru_pts_delta[0], stream->lru_pts_delta[1], stream->lru_pts_delta[2]); | |
456 | 1170 |
1171 *key_frame_ret= !!(flags & FLAG_KEY_FRAME); | |
403 | 1172 |
461 | 1173 if(!time_delta){ |
456 | 1174 int64_t mask = (1<<stream->msb_timestamp_shift)-1; |
1175 pts= get_v(bc); | |
1176 if(pts > mask){ | |
1177 pts -= mask+1; | |
1178 }else{ | |
1179 if(stream->last_pts == AV_NOPTS_VALUE){ | |
1180 av_log(s, AV_LOG_ERROR, "no reference pts available\n"); | |
1181 return -1; | |
426 | 1182 } |
456 | 1183 pts= lsb2full(stream, pts); |
403 | 1184 } |
1185 }else{ | |
456 | 1186 if(stream->last_pts == AV_NOPTS_VALUE){ |
1187 av_log(s, AV_LOG_ERROR, "no reference pts available\n"); | |
1188 return -1; | |
1189 } | |
461 | 1190 pts= stream->last_pts + time_delta; |
403 | 1191 } |
456 | 1192 |
1193 if(*key_frame_ret){ | |
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1400
diff
changeset
|
1194 // av_log(s, AV_LOG_DEBUG, "stream:%d start:%"PRId64" pts:%"PRId64" length:%"PRId64"\n",stream_id, frame_start, av_pts, frame_start - nut->stream[stream_id].last_sync_pos); |
456 | 1195 av_add_index_entry( |
885 | 1196 s->streams[stream_id], |
1197 frame_start, | |
1198 pts, | |
979 | 1199 0, |
456 | 1200 frame_start - nut->stream[stream_id].last_sync_pos, |
1201 AVINDEX_KEYFRAME); | |
1202 nut->stream[stream_id].last_sync_pos= frame_start; | |
1203 // assert(nut->packet_start == frame_start); | |
403 | 1204 } |
456 | 1205 |
1206 assert(size_mul > size_lsb); | |
1207 size= size_lsb; | |
1208 if(flags & FLAG_DATA_SIZE) | |
1209 size+= size_mul*get_v(bc); | |
885 | 1210 |
461 | 1211 #ifdef TRACE |
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1400
diff
changeset
|
1212 av_log(s, AV_LOG_DEBUG, "fs:%"PRId64" fc:%d ft:%d kf:%d pts:%"PRId64" size:%d mul:%d lsb:%d flags:%d delta:%d\n", frame_start, frame_code, frame_type, *key_frame_ret, pts, size, size_mul, size_lsb, flags, time_delta); |
461 | 1213 #endif |
421 | 1214 |
456 | 1215 if(frame_type==0 && url_ftell(bc) - nut->packet_start[2] + size > nut->max_distance){ |
421 | 1216 av_log(s, AV_LOG_ERROR, "frame size too large\n"); |
1217 return -1; | |
1218 } | |
885 | 1219 |
456 | 1220 *stream_id_ret = stream_id; |
463
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1221 *pts_ret = pts; |
456 | 1222 |
1223 update(nut, stream_id, frame_start, frame_type, frame_code, *key_frame_ret, size, pts); | |
1224 | |
1225 return size; | |
1226 } | |
1227 | |
461 | 1228 static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code, int frame_type, int64_t frame_start){ |
456 | 1229 AVFormatContext *s= nut->avf; |
1230 ByteIOContext *bc = &s->pb; | |
708 | 1231 int size, stream_id, key_frame, discard; |
1232 int64_t pts, last_IP_pts; | |
885 | 1233 |
461 | 1234 size= decode_frame_header(nut, &key_frame, &pts, &stream_id, frame_code, frame_type, frame_start); |
456 | 1235 if(size < 0) |
1236 return -1; | |
1237 | |
708 | 1238 discard= s->streams[ stream_id ]->discard; |
1239 last_IP_pts= s->streams[ stream_id ]->last_IP_pts; | |
1240 if( (discard >= AVDISCARD_NONKEY && !key_frame) | |
1241 ||(discard >= AVDISCARD_BIDIR && last_IP_pts != AV_NOPTS_VALUE && last_IP_pts > pts) | |
1242 || discard >= AVDISCARD_ALL){ | |
652 | 1243 url_fskip(bc, size); |
1244 return 1; | |
1245 } | |
1246 | |
775 | 1247 av_get_packet(bc, pkt, size); |
403 | 1248 pkt->stream_index = stream_id; |
219 | 1249 if (key_frame) |
887 | 1250 pkt->flags |= PKT_FLAG_KEY; |
456 | 1251 pkt->pts = pts; |
403 | 1252 |
421 | 1253 return 0; |
1254 } | |
403 | 1255 |
421 | 1256 static int nut_read_packet(AVFormatContext *s, AVPacket *pkt) |
1257 { | |
1258 NUTContext *nut = s->priv_data; | |
1259 ByteIOContext *bc = &s->pb; | |
652 | 1260 int i, frame_code=0, ret; |
421 | 1261 |
1262 for(;;){ | |
461 | 1263 int64_t pos= url_ftell(bc); |
426 | 1264 int frame_type= 0; |
421 | 1265 uint64_t tmp= nut->next_startcode; |
1266 nut->next_startcode=0; | |
1267 | |
1268 if (url_feof(bc)) | |
1269 return -1; | |
1270 | |
461 | 1271 if(tmp){ |
1272 pos-=8; | |
1273 }else{ | |
421 | 1274 frame_code = get_byte(bc); |
1275 if(frame_code == 'N'){ | |
1276 tmp= frame_code; | |
426 | 1277 for(i=1; i<8; i++) |
1278 tmp = (tmp<<8) + get_byte(bc); | |
421 | 1279 } |
1280 } | |
1281 switch(tmp){ | |
1282 case MAIN_STARTCODE: | |
1283 case STREAM_STARTCODE: | |
1284 case INDEX_STARTCODE: | |
456 | 1285 get_packetheader(nut, bc, 0); |
461 | 1286 assert(nut->packet_start[2] == pos); |
853 | 1287 url_fseek(bc, nut->written_packet_size, SEEK_CUR); |
421 | 1288 break; |
1289 case INFO_STARTCODE: | |
1290 if(decode_info_header(nut)<0) | |
1291 goto resync; | |
1292 break; | |
426 | 1293 case KEYFRAME_STARTCODE: |
1294 frame_type = 2; | |
461 | 1295 reset(s, get_v(bc)); |
426 | 1296 frame_code = get_byte(bc); |
421 | 1297 case 0: |
652 | 1298 ret= decode_frame(nut, pkt, frame_code, frame_type, pos); |
1299 if(ret==0) | |
421 | 1300 return 0; |
652 | 1301 else if(ret==1) //ok but discard packet |
1302 break; | |
421 | 1303 default: |
1304 resync: | |
881 | 1305 av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", nut->packet_start[2]+1); |
456 | 1306 tmp= find_any_startcode(bc, nut->packet_start[2]+1); |
421 | 1307 if(tmp==0) |
1308 return -1; | |
1309 av_log(s, AV_LOG_DEBUG, "sync\n"); | |
456 | 1310 nut->next_startcode= tmp; |
426 | 1311 } |
1312 } | |
1313 } | |
1314 | |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
432
diff
changeset
|
1315 static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index, int64_t *pos_arg, int64_t pos_limit){ |
426 | 1316 NUTContext *nut = s->priv_data; |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
432
diff
changeset
|
1317 StreamContext *stream; |
426 | 1318 ByteIOContext *bc = &s->pb; |
1319 int64_t pos, pts; | |
1320 uint64_t code; | |
456 | 1321 int frame_code,step, stream_id, i,size, key_frame; |
881 | 1322 av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n", stream_index, *pos_arg, pos_limit); |
426 | 1323 |
1324 if(*pos_arg < 0) | |
1325 return AV_NOPTS_VALUE; | |
1326 | |
1327 pos= *pos_arg; | |
1328 step= FFMIN(16*1024, pos); | |
1329 do{ | |
1330 pos-= step; | |
1331 code= find_any_startcode(bc, pos); | |
1332 | |
456 | 1333 if(code && url_ftell(bc) - 8 <= *pos_arg) |
426 | 1334 break; |
1335 step= FFMIN(2*step, pos); | |
1336 }while(step); | |
1337 | |
1338 if(!code) //nothing found, not even after pos_arg | |
1339 return AV_NOPTS_VALUE; | |
1340 | |
1341 url_fseek(bc, -8, SEEK_CUR); | |
1342 for(i=0; i<s->nb_streams; i++) | |
1343 nut->stream[i].last_sync_pos= url_ftell(bc); | |
885 | 1344 |
426 | 1345 for(;;){ |
456 | 1346 int frame_type=0; |
426 | 1347 int64_t pos= url_ftell(bc); |
1348 uint64_t tmp=0; | |
885 | 1349 |
456 | 1350 if(pos > pos_limit || url_feof(bc)) |
426 | 1351 return AV_NOPTS_VALUE; |
1352 | |
1353 frame_code = get_byte(bc); | |
1354 if(frame_code == 'N'){ | |
1355 tmp= frame_code; | |
1356 for(i=1; i<8; i++) | |
1357 tmp = (tmp<<8) + get_byte(bc); | |
1358 } | |
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1400
diff
changeset
|
1359 //av_log(s, AV_LOG_DEBUG, "before switch %"PRIX64" at=%"PRId64"\n", tmp, pos); |
426 | 1360 |
1361 switch(tmp){ | |
1362 case MAIN_STARTCODE: | |
1363 case STREAM_STARTCODE: | |
1364 case INDEX_STARTCODE: | |
1365 case INFO_STARTCODE: | |
456 | 1366 get_packetheader(nut, bc, 0); |
1367 assert(nut->packet_start[2]==pos); | |
853 | 1368 url_fseek(bc, nut->written_packet_size, SEEK_CUR); |
426 | 1369 break; |
1370 case KEYFRAME_STARTCODE: | |
456 | 1371 frame_type=2; |
461 | 1372 reset(s, get_v(bc)); |
426 | 1373 frame_code = get_byte(bc); |
1374 case 0: | |
461 | 1375 size= decode_frame_header(nut, &key_frame, &pts, &stream_id, frame_code, frame_type, pos); |
456 | 1376 if(size < 0) |
426 | 1377 goto resync; |
885 | 1378 |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
432
diff
changeset
|
1379 stream= &nut->stream[stream_id]; |
456 | 1380 if(stream_id != stream_index || !key_frame || pos < *pos_arg){ |
1381 url_fseek(bc, size, SEEK_CUR); | |
426 | 1382 break; |
1383 } | |
885 | 1384 |
456 | 1385 *pos_arg= pos; |
426 | 1386 return pts; |
1387 default: | |
1388 resync: | |
881 | 1389 av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", nut->packet_start[2]+1); |
456 | 1390 if(!find_any_startcode(bc, nut->packet_start[2]+1)) |
426 | 1391 return AV_NOPTS_VALUE; |
1392 | |
1393 url_fseek(bc, -8, SEEK_CUR); | |
421 | 1394 } |
1395 } | |
426 | 1396 return AV_NOPTS_VALUE; |
1397 } | |
1398 | |
555 | 1399 static int nut_read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){ |
456 | 1400 // NUTContext *nut = s->priv_data; |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
432
diff
changeset
|
1401 int64_t pos; |
426 | 1402 |
555 | 1403 if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0) |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
432
diff
changeset
|
1404 return -1; |
426 | 1405 |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
432
diff
changeset
|
1406 pos= url_ftell(&s->pb); |
456 | 1407 nut_read_timestamp(s, stream_index, &pos, pos-1); |
426 | 1408 |
1409 return 0; | |
403 | 1410 } |
1411 | |
1412 static int nut_read_close(AVFormatContext *s) | |
1413 { | |
1414 NUTContext *nut = s->priv_data; | |
1415 | |
1416 av_freep(&nut->stream); | |
219 | 1417 |
1418 return 0; | |
1419 } | |
1420 | |
1169 | 1421 #ifdef CONFIG_NUT_DEMUXER |
1422 AVInputFormat nut_demuxer = { | |
219 | 1423 "nut", |
1424 "nut format", | |
1425 sizeof(NUTContext), | |
1426 nut_probe, | |
1427 nut_read_header, | |
1428 nut_read_packet, | |
403 | 1429 nut_read_close, |
426 | 1430 nut_read_seek, |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
432
diff
changeset
|
1431 nut_read_timestamp, |
219 | 1432 .extensions = "nut", |
1433 }; | |
1169 | 1434 #endif |
1435 #ifdef CONFIG_NUT_MUXER | |
1436 AVOutputFormat nut_muxer = { | |
219 | 1437 "nut", |
1438 "nut format", | |
1439 "video/x-nut", | |
1440 "nut", | |
1441 sizeof(NUTContext), | |
682 | 1442 #ifdef CONFIG_LIBVORBIS |
219 | 1443 CODEC_ID_VORBIS, |
1622 | 1444 #elif defined(CONFIG_LIBMP3LAME) |
232 | 1445 CODEC_ID_MP3, |
219 | 1446 #else |
223 | 1447 CODEC_ID_MP2, /* AC3 needs liba52 decoder */ |
219 | 1448 #endif |
1449 CODEC_ID_MPEG4, | |
1450 nut_write_header, | |
1451 nut_write_packet, | |
1452 nut_write_trailer, | |
798 | 1453 .flags = AVFMT_GLOBALHEADER, |
219 | 1454 }; |
1169 | 1455 #endif |