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