Mercurial > libavformat.hg
annotate nut.c @ 445:c1f88671135b libavformat
cleanup
fix streamcopy
author | michael |
---|---|
date | Sat, 17 Apr 2004 12:04:59 +0000 |
parents | e17fb306b107 |
children | c13fd248e222 |
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 */ | |
441 | 616 if(!(s->streams[0]->codec.flags & CODEC_FLAG_BITEXACT)){ |
617 put_v(bc, 13); /* type */ | |
618 put_str(bc, LIBAVFORMAT_IDENT); | |
619 } | |
222 | 620 |
621 put_v(bc, 0); /* eof info */ | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
622 update_packetheader(nut, bc, 0, 1); |
219 | 623 |
624 put_flush_packet(bc); | |
625 | |
626 return 0; | |
627 } | |
628 | |
629 static int nut_write_packet(AVFormatContext *s, int stream_index, | |
241 | 630 const uint8_t *buf, int size, int64_t pts) |
219 | 631 { |
632 NUTContext *nut = s->priv_data; | |
403 | 633 StreamContext *stream= &nut->stream[stream_index]; |
219 | 634 ByteIOContext *bc = &s->pb; |
403 | 635 int key_frame = 0, full_pts=0; |
219 | 636 AVCodecContext *enc; |
403 | 637 int64_t lsb_pts, delta_pts; |
638 int frame_type, best_length, frame_code, flags, i, size_mul, size_lsb; | |
639 const int64_t frame_start= url_ftell(bc); | |
219 | 640 |
641 if (stream_index > s->nb_streams) | |
642 return 1; | |
403 | 643 |
644 pts= (av_rescale(pts, stream->rate_num, stream->rate_den) + AV_TIME_BASE/2) / AV_TIME_BASE; | |
219 | 645 |
646 enc = &s->streams[stream_index]->codec; | |
403 | 647 key_frame = enc->coded_frame->key_frame; |
442 | 648 if(enc->coded_frame->pts != AV_NOPTS_VALUE) |
649 pts= (av_rescale(enc->coded_frame->pts, stream->rate_num, stream->rate_den) + AV_TIME_BASE/2) / AV_TIME_BASE; //FIXME XXX HACK | |
403 | 650 delta_pts= pts - stream->last_pts; |
651 | |
652 frame_type=0; | |
653 if(frame_start + size + 20 - FFMAX(nut->last_frame_start[1], nut->last_frame_start[2]) > MAX_TYPE1_DISTANCE) | |
654 frame_type=1; | |
655 if(key_frame){ | |
656 if(frame_type==1 && frame_start + size - nut->last_frame_start[2] > MAX_TYPE2_DISTANCE) | |
657 frame_type=2; | |
658 if(!stream->last_key_frame) | |
659 frame_type=2; | |
328 | 660 } |
661 | |
403 | 662 if(frame_type>0){ |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
663 update_packetheader(nut, bc, 0, 0); |
403 | 664 reset(s); |
665 full_pts=1; | |
666 } | |
667 //FIXME ensure that the timestamp can be represented by either delta or lsb or full_pts=1 | |
668 | |
669 lsb_pts = pts & ((1 << stream->msb_timestamp_shift)-1); | |
670 | |
671 best_length=INT_MAX; | |
672 frame_code= -1; | |
673 for(i=0; i<256; i++){ | |
674 int stream_id_plus1= nut->frame_code[i].stream_id_plus1; | |
429
983639863758
removing keyframe prediction and checksum threshold
michael
parents:
427
diff
changeset
|
675 int fc_key_frame; |
403 | 676 int length=0; |
677 size_mul= nut->frame_code[i].size_mul; | |
678 size_lsb= nut->frame_code[i].size_lsb; | |
679 flags= nut->frame_code[i].flags; | |
680 | |
681 if(stream_id_plus1 == 0) length+= get_length(stream_index); | |
682 else if(stream_id_plus1 - 1 != stream_index) | |
683 continue; | |
429
983639863758
removing keyframe prediction and checksum threshold
michael
parents:
427
diff
changeset
|
684 fc_key_frame= !!(flags & FLAG_KEY_FRAME); |
983639863758
removing keyframe prediction and checksum threshold
michael
parents:
427
diff
changeset
|
685 |
403 | 686 assert(key_frame==0 || key_frame==1); |
687 if(fc_key_frame != key_frame) | |
688 continue; | |
689 | |
690 if((!!(flags & FLAG_FRAME_TYPE)) != (frame_type > 0)) | |
691 continue; | |
692 | |
693 if(size_mul <= size_lsb){ | |
694 int p= stream->lru_size[size_lsb - size_mul]; | |
695 if(p != size) | |
696 continue; | |
697 }else{ | |
698 if(size % size_mul != size_lsb) | |
699 continue; | |
700 if(flags & FLAG_DATA_SIZE) | |
701 length += get_length(size / size_mul); | |
702 else if(size/size_mul) | |
703 continue; | |
704 } | |
220 | 705 |
403 | 706 if(full_pts != ((flags & FLAG_PTS) && (flags & FLAG_FULL_PTS))) |
707 continue; | |
708 | |
709 if(flags&FLAG_PTS){ | |
710 if(flags&FLAG_FULL_PTS){ | |
711 length += get_length(pts); | |
712 }else{ | |
713 length += get_length(lsb_pts); | |
714 } | |
715 }else{ | |
716 int delta= stream->lru_pts_delta[(flags & 12)>>2]; | |
717 if(delta != pts - stream->last_pts) | |
718 continue; | |
719 assert(frame_type == 0); | |
720 } | |
721 | |
722 if(length < best_length){ | |
723 best_length= length; | |
724 frame_code=i; | |
725 } | |
726 // 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); | |
727 } | |
223 | 728 |
403 | 729 assert(frame_code != -1); |
730 flags= nut->frame_code[frame_code].flags; | |
731 size_mul= nut->frame_code[frame_code].size_mul; | |
732 size_lsb= nut->frame_code[frame_code].size_lsb; | |
733 #if 0 | |
734 best_length /= 7; | |
735 best_length ++; //frame_code | |
736 if(frame_type>0){ | |
737 best_length += 4; //packet header | |
738 if(frame_type>1) | |
739 best_length += 8; // startcode | |
740 } | |
741 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)); | |
742 #endif | |
743 | |
744 if (frame_type==2) | |
745 put_be64(bc, KEYFRAME_STARTCODE); | |
746 put_byte(bc, frame_code); | |
747 | |
748 if(frame_type>0) | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
749 put_packetheader(nut, bc, FFMAX(size+20, MAX_TYPE1_DISTANCE), 0); |
403 | 750 if(nut->frame_code[frame_code].stream_id_plus1 == 0) |
751 put_v(bc, stream_index); | |
752 if (flags & FLAG_PTS){ | |
753 if (flags & FLAG_FULL_PTS) | |
754 put_v(bc, pts); | |
755 else | |
756 put_v(bc, lsb_pts); | |
757 } | |
758 if(flags & FLAG_DATA_SIZE) | |
759 put_v(bc, size / size_mul); | |
760 if(size > MAX_TYPE1_DISTANCE){ | |
761 assert(frame_type > 0); | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
762 update_packetheader(nut, bc, size, 0); |
403 | 763 } |
219 | 764 |
765 put_buffer(bc, buf, size); | |
403 | 766 |
767 update(nut, stream_index, frame_start, frame_type, frame_code, key_frame, size, pts); | |
219 | 768 |
769 return 0; | |
770 } | |
771 | |
772 static int nut_write_trailer(AVFormatContext *s) | |
773 { | |
328 | 774 NUTContext *nut = s->priv_data; |
219 | 775 ByteIOContext *bc = &s->pb; |
403 | 776 |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
777 update_packetheader(nut, bc, 0, 0); |
403 | 778 |
219 | 779 #if 0 |
780 int i; | |
781 | |
782 /* WRITE INDEX */ | |
783 | |
784 for (i = 0; s->nb_streams; i++) | |
785 { | |
223 | 786 put_be64(bc, INDEX_STARTCODE); |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
787 put_packetheader(nut, bc, 64, 1); |
219 | 788 put_v(bc, s->streams[i]->id); |
789 put_v(bc, ...); | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
790 update_packetheader(nut, bc, 0, 1); |
219 | 791 } |
792 #endif | |
793 | |
794 put_flush_packet(bc); | |
328 | 795 |
403 | 796 av_freep(&nut->stream); |
219 | 797 |
798 return 0; | |
799 } | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
272
diff
changeset
|
800 #endif //CONFIG_ENCODERS |
219 | 801 |
802 static int nut_probe(AVProbeData *p) | |
803 { | |
220 | 804 int i; |
805 uint64_t code; | |
806 | |
807 code = 0xff; | |
808 for (i = 0; i < p->buf_size; i++) { | |
809 int c = p->buf[i]; | |
810 code = (code << 8) | c; | |
811 if (code == MAIN_STARTCODE) | |
812 return AVPROBE_SCORE_MAX; | |
813 } | |
814 return 0; | |
219 | 815 } |
816 | |
419 | 817 static int decode_main_header(NUTContext *nut){ |
818 AVFormatContext *s= nut->avf; | |
219 | 819 ByteIOContext *bc = &s->pb; |
220 | 820 uint64_t tmp; |
419 | 821 int i, j; |
219 | 822 |
419 | 823 get_packetheader(nut, bc, 8, 1); |
403 | 824 |
419 | 825 tmp = get_v(bc); |
826 if (tmp != 1){ | |
827 av_log(s, AV_LOG_ERROR, "bad version (%Ld)\n", tmp); | |
828 return -1; | |
829 } | |
219 | 830 |
419 | 831 nut->stream_count = get_v(bc); |
403 | 832 |
833 for(i=0; i<256;){ | |
834 int tmp_flags = get_v(bc); | |
835 int tmp_stream= get_v(bc); | |
836 int tmp_mul = get_v(bc); | |
837 int tmp_size = get_v(bc); | |
838 int count = get_v(bc); | |
839 | |
840 if(count == 0 || i+count > 256){ | |
841 av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i); | |
842 return -1; | |
843 } | |
844 | |
845 if((tmp_flags & FLAG_FRAME_TYPE) && tmp_flags != 1){ | |
846 if(!(tmp_flags & FLAG_PTS) || !(tmp_flags & FLAG_FULL_PTS) ){ | |
847 av_log(s, AV_LOG_ERROR, "no full pts in non 0 frame type\n"); | |
848 return -1; | |
849 } | |
850 } | |
851 | |
852 for(j=0; j<count; j++,i++){ | |
419 | 853 if(tmp_stream > nut->stream_count + 1){ |
403 | 854 av_log(s, AV_LOG_ERROR, "illegal stream number\n"); |
855 return -1; | |
856 } | |
857 | |
858 nut->frame_code[i].flags = tmp_flags ; | |
859 nut->frame_code[i].stream_id_plus1 = tmp_stream; | |
860 nut->frame_code[i].size_mul = tmp_mul ; | |
861 nut->frame_code[i].size_lsb = tmp_size ; | |
862 if(++tmp_size >= tmp_mul){ | |
863 tmp_size=0; | |
864 tmp_stream++; | |
865 } | |
866 } | |
867 } | |
868 if(nut->frame_code['N'].flags != 1){ | |
869 av_log(s, AV_LOG_ERROR, "illegal frame_code table\n"); | |
870 return -1; | |
871 } | |
419 | 872 |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
873 if(check_checksum(bc)){ |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
874 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
|
875 return -1; |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
876 } |
419 | 877 |
878 return 0; | |
879 } | |
880 | |
881 static int decode_stream_header(NUTContext *nut){ | |
882 AVFormatContext *s= nut->avf; | |
883 ByteIOContext *bc = &s->pb; | |
884 int class, nom, denom, stream_id, i; | |
885 uint64_t tmp; | |
886 AVStream *st; | |
887 | |
888 get_packetheader(nut, bc, 8, 1); | |
889 stream_id= get_v(bc); | |
890 if(stream_id >= nut->stream_count || s->streams[stream_id]) | |
891 return -1; | |
892 | |
893 st = av_new_stream(s, stream_id); | |
894 if (!st) | |
895 return AVERROR_NOMEM; | |
896 class = get_v(bc); | |
426 | 897 tmp = get_vb(bc); |
432 | 898 st->codec.codec_tag= tmp; |
419 | 899 switch(class) |
900 { | |
901 case 0: | |
902 st->codec.codec_type = CODEC_TYPE_VIDEO; | |
903 st->codec.codec_id = codec_get_bmp_id(tmp); | |
904 if (st->codec.codec_id == CODEC_ID_NONE) | |
905 av_log(s, AV_LOG_ERROR, "Unknown codec?!\n"); | |
906 break; | |
907 case 32: | |
908 st->codec.codec_type = CODEC_TYPE_AUDIO; | |
909 st->codec.codec_id = codec_get_wav_id(tmp); | |
910 if (st->codec.codec_id == CODEC_ID_NONE) | |
911 av_log(s, AV_LOG_ERROR, "Unknown codec?!\n"); | |
912 break; | |
913 default: | |
914 av_log(s, AV_LOG_ERROR, "Unknown stream class (%d)\n", class); | |
915 return -1; | |
916 } | |
917 s->bit_rate += get_v(bc); | |
426 | 918 get_vb(bc); /* language code */ |
419 | 919 nom = get_v(bc); |
920 denom = get_v(bc); | |
921 nut->stream[stream_id].msb_timestamp_shift = get_v(bc); | |
922 for(i=0; i<3; i++) | |
923 nut->stream[stream_id].initial_pts_predictor[i]= get_v(bc); | |
924 for(i=0; i<2; i++) | |
925 nut->stream[stream_id].initial_size_predictor[i]= get_v(bc); | |
926 get_byte(bc); /* flags */ | |
927 | |
928 /* codec specific data headers */ | |
929 while(get_v(bc) != 0){ | |
930 st->codec.extradata_size= get_v(bc); | |
931 st->codec.extradata= av_mallocz(st->codec.extradata_size); | |
932 get_buffer(bc, st->codec.extradata, st->codec.extradata_size); | |
933 // url_fskip(bc, get_v(bc)); | |
934 } | |
935 | |
936 if (class == 0) /* VIDEO */ | |
937 { | |
938 st->codec.width = get_v(bc); | |
939 st->codec.height = get_v(bc); | |
940 st->codec.sample_aspect_ratio.num= get_v(bc); | |
941 st->codec.sample_aspect_ratio.den= get_v(bc); | |
942 get_v(bc); /* csp type */ | |
943 | |
944 st->codec.frame_rate = nom; | |
945 st->codec.frame_rate_base = denom; | |
946 } | |
947 if (class == 32) /* AUDIO */ | |
948 { | |
949 st->codec.sample_rate = (get_v(bc) * nom) / denom; | |
950 st->codec.channels = get_v(bc); | |
951 } | |
952 if(check_checksum(bc)){ | |
953 av_log(s, AV_LOG_ERROR, "Stream header %d checksum missmatch\n", stream_id); | |
954 return -1; | |
955 } | |
956 nut->stream[stream_id].rate_num= nom; | |
957 nut->stream[stream_id].rate_den= denom; | |
958 return 0; | |
959 } | |
960 | |
961 static int decode_info_header(NUTContext *nut){ | |
962 AVFormatContext *s= nut->avf; | |
963 ByteIOContext *bc = &s->pb; | |
964 | |
965 get_packetheader(nut, bc, 8, 1); | |
966 | |
967 for(;;){ | |
968 int id= get_v(bc); | |
969 char *name, *type, custom_name[256], custom_type[256]; | |
970 | |
971 if(!id) | |
972 break; | |
973 else if(id >= sizeof(info_table)/sizeof(info_table[0])){ | |
974 av_log(s, AV_LOG_ERROR, "info id is too large %d %d\n", id, sizeof(info_table)/sizeof(info_table[0])); | |
975 return -1; | |
976 } | |
977 | |
978 type= info_table[id][1]; | |
979 name= info_table[id][0]; | |
980 //av_log(s, AV_LOG_DEBUG, "%d %s %s\n", id, type, name); | |
981 | |
982 if(!type){ | |
983 get_str(bc, custom_type, sizeof(custom_type)); | |
984 type= custom_type; | |
985 } | |
986 if(!name){ | |
987 get_str(bc, custom_name, sizeof(custom_name)); | |
988 name= custom_name; | |
989 } | |
990 | |
991 if(!strcmp(type, "v")){ | |
992 int value= get_v(bc); | |
993 }else{ | |
994 if(!strcmp(name, "Author")) | |
995 get_str(bc, s->author, sizeof(s->author)); | |
996 else if(!strcmp(name, "Title")) | |
997 get_str(bc, s->title, sizeof(s->title)); | |
998 else if(!strcmp(name, "Copyright")) | |
999 get_str(bc, s->copyright, sizeof(s->copyright)); | |
1000 else if(!strcmp(name, "Description")) | |
1001 get_str(bc, s->comment, sizeof(s->comment)); | |
1002 else | |
1003 get_str(bc, NULL, 0); | |
1004 } | |
1005 } | |
1006 if(check_checksum(bc)){ | |
1007 av_log(s, AV_LOG_ERROR, "Info header checksum missmatch\n"); | |
1008 return -1; | |
1009 } | |
1010 return 0; | |
1011 } | |
1012 | |
1013 static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap) | |
1014 { | |
1015 NUTContext *nut = s->priv_data; | |
1016 ByteIOContext *bc = &s->pb; | |
1017 int64_t pos; | |
1018 int inited_stream_count; | |
1019 | |
1020 nut->avf= s; | |
1021 | |
1022 av_set_pts_info(s, 60, 1, AV_TIME_BASE); | |
1023 | |
1024 /* main header */ | |
1025 pos=0; | |
1026 for(;;){ | |
426 | 1027 pos= find_startcode(bc, MAIN_STARTCODE, pos)+1; |
1028 if (pos<0){ | |
419 | 1029 av_log(s, AV_LOG_ERROR, "no main startcode found\n"); |
1030 return -1; | |
1031 } | |
1032 if(decode_main_header(nut) >= 0) | |
1033 break; | |
1034 } | |
1035 | |
219 | 1036 |
1037 s->bit_rate = 0; | |
328 | 1038 |
419 | 1039 nut->stream = av_malloc(sizeof(StreamContext)*nut->stream_count); |
272 | 1040 |
419 | 1041 /* stream headers */ |
1042 pos=0; | |
1043 for(inited_stream_count=0; inited_stream_count < nut->stream_count;){ | |
426 | 1044 pos= find_startcode(bc, STREAM_STARTCODE, pos)+1; |
1045 if (pos<0){ | |
419 | 1046 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
|
1047 return -1; |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
416
diff
changeset
|
1048 } |
419 | 1049 if(decode_stream_header(nut) >= 0) |
1050 inited_stream_count++; | |
415 | 1051 } |
1052 | |
419 | 1053 /* info headers */ |
1054 pos=0; | |
1055 for(;;){ | |
1056 uint64_t startcode= find_any_startcode(bc, pos); | |
1057 pos= url_ftell(bc); | |
415 | 1058 |
419 | 1059 if(startcode==0){ |
1060 av_log(s, AV_LOG_ERROR, "EOF before video frames\n"); | |
1061 return -1; | |
1062 }else if(startcode == KEYFRAME_STARTCODE){ | |
421 | 1063 nut->next_startcode= startcode; |
419 | 1064 break; |
1065 }else if(startcode != INFO_STARTCODE){ | |
1066 continue; | |
415 | 1067 } |
419 | 1068 |
1069 decode_info_header(nut); | |
1070 } | |
1071 | |
219 | 1072 return 0; |
1073 } | |
1074 | |
421 | 1075 static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code, int frame_type){ |
1076 AVFormatContext *s= nut->avf; | |
403 | 1077 StreamContext *stream; |
219 | 1078 ByteIOContext *bc = &s->pb; |
421 | 1079 int size, flags, size_mul, size_lsb, stream_id; |
219 | 1080 int key_frame = 0; |
328 | 1081 int64_t pts = 0; |
421 | 1082 const int prefix_len= frame_type == 2 ? 8+1 : 1; |
426 | 1083 const int64_t frame_start= url_ftell(bc) - prefix_len; |
219 | 1084 |
403 | 1085 flags= nut->frame_code[frame_code].flags; |
1086 size_mul= nut->frame_code[frame_code].size_mul; | |
1087 size_lsb= nut->frame_code[frame_code].size_lsb; | |
1088 stream_id= nut->frame_code[frame_code].stream_id_plus1 - 1; | |
1089 | |
1090 if(flags & FLAG_FRAME_TYPE){ | |
1091 reset(s); | |
421 | 1092 if(get_packetheader(nut, bc, prefix_len, 0) < 0) |
1093 return -1; | |
1094 if(frame_type!=2) | |
403 | 1095 frame_type= 1; |
1096 } | |
1097 | |
1098 if(stream_id==-1) | |
1099 stream_id= get_v(bc); | |
1100 if(stream_id >= s->nb_streams){ | |
1101 av_log(s, AV_LOG_ERROR, "illegal stream_id\n"); | |
1102 return -1; | |
1103 } | |
1104 stream= &nut->stream[stream_id]; | |
421 | 1105 |
1106 // 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 | 1107 |
429
983639863758
removing keyframe prediction and checksum threshold
michael
parents:
427
diff
changeset
|
1108 key_frame= !!(flags & FLAG_KEY_FRAME); |
403 | 1109 |
1110 if(flags & FLAG_PTS){ | |
1111 if(flags & FLAG_FULL_PTS){ | |
1112 pts= get_v(bc); | |
426 | 1113 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
|
1114 int64_t av_pts= pts * AV_TIME_BASE * stream->rate_den / stream->rate_num; |
426 | 1115 av_add_index_entry( |
1116 s->streams[stream_id], | |
1117 frame_start, | |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
432
diff
changeset
|
1118 av_pts, |
426 | 1119 frame_start - nut->stream[stream_id].last_sync_pos, |
1120 AVINDEX_KEYFRAME); | |
1121 nut->stream[stream_id].last_sync_pos= frame_start; | |
1122 assert(nut->packet_start == frame_start); | |
1123 } | |
403 | 1124 }else{ |
1125 int64_t mask = (1<<stream->msb_timestamp_shift)-1; | |
1126 int64_t delta= stream->last_pts - mask/2; | |
1127 pts= ((get_v(bc) - delta)&mask) + delta; | |
1128 } | |
1129 }else{ | |
1130 pts= stream->last_pts + stream->lru_pts_delta[(flags&12)>>2]; | |
1131 } | |
1132 | |
1133 if(size_mul <= size_lsb){ | |
1134 size= stream->lru_size[size_lsb - size_mul]; | |
1135 }else{ | |
1136 if(flags & FLAG_DATA_SIZE) | |
1137 size= size_mul*get_v(bc) + size_lsb; | |
1138 else | |
1139 size= size_lsb; | |
1140 } | |
1141 | |
421 | 1142 //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); |
1143 | |
1144 if(url_ftell(bc) - nut->packet_start + size > nut->written_packet_size){ | |
1145 av_log(s, AV_LOG_ERROR, "frame size too large\n"); | |
1146 return -1; | |
1147 } | |
219 | 1148 |
1149 av_new_packet(pkt, size); | |
1150 get_buffer(bc, pkt->data, size); | |
403 | 1151 pkt->stream_index = stream_id; |
219 | 1152 if (key_frame) |
1153 pkt->flags |= PKT_FLAG_KEY; | |
403 | 1154 pkt->pts = pts * AV_TIME_BASE * stream->rate_den / stream->rate_num; |
1155 | |
1156 update(nut, stream_id, frame_start, frame_type, frame_code, key_frame, size, pts); | |
421 | 1157 |
1158 return 0; | |
1159 } | |
403 | 1160 |
421 | 1161 static int nut_read_packet(AVFormatContext *s, AVPacket *pkt) |
1162 { | |
1163 NUTContext *nut = s->priv_data; | |
1164 ByteIOContext *bc = &s->pb; | |
426 | 1165 int size, i, frame_code=0; |
421 | 1166 int64_t pos; |
1167 | |
1168 for(;;){ | |
426 | 1169 int frame_type= 0; |
421 | 1170 uint64_t tmp= nut->next_startcode; |
1171 nut->next_startcode=0; | |
1172 | |
1173 if (url_feof(bc)) | |
1174 return -1; | |
1175 | |
1176 if(!tmp){ | |
1177 frame_code = get_byte(bc); | |
1178 if(frame_code == 'N'){ | |
1179 tmp= frame_code; | |
426 | 1180 for(i=1; i<8; i++) |
1181 tmp = (tmp<<8) + get_byte(bc); | |
421 | 1182 } |
1183 } | |
1184 switch(tmp){ | |
1185 case MAIN_STARTCODE: | |
1186 case STREAM_STARTCODE: | |
1187 case INDEX_STARTCODE: | |
1188 get_packetheader(nut, bc, 8, 0); | |
1189 url_fseek(bc, nut->written_packet_size + nut->packet_start, SEEK_SET); | |
1190 break; | |
1191 case INFO_STARTCODE: | |
1192 if(decode_info_header(nut)<0) | |
1193 goto resync; | |
1194 break; | |
426 | 1195 case KEYFRAME_STARTCODE: |
1196 frame_type = 2; | |
1197 frame_code = get_byte(bc); | |
421 | 1198 case 0: |
1199 if(decode_frame(nut, pkt, frame_code, frame_type)>=0) | |
1200 return 0; | |
1201 default: | |
1202 resync: | |
1203 av_log(s, AV_LOG_DEBUG, "syncing from %lld\n", nut->packet_start+1); | |
1204 tmp= find_any_startcode(bc, nut->packet_start+1); | |
1205 if(tmp==0) | |
1206 return -1; | |
1207 av_log(s, AV_LOG_DEBUG, "sync\n"); | |
1208 if(url_is_streamed(bc)){ | |
1209 nut->next_startcode= tmp; | |
1210 break; | |
1211 } | |
1212 | |
1213 pos= url_ftell(bc) - 8; | |
1214 av_log(s, AV_LOG_DEBUG, "at %lld code=%llX\n", pos, tmp); | |
1215 if(tmp==KEYFRAME_STARTCODE){ | |
1216 get_byte(bc); | |
1217 } | |
1218 get_v(bc); | |
1219 size= get_v(bc); | |
1220 | |
1221 while(size > 2 && size < 100000 && nut->packet_start < pos - size){ | |
1222 url_fseek(bc, pos - size, SEEK_SET); | |
1223 frame_code= get_byte(bc); | |
1224 if(!(nut->frame_code[ frame_code ].flags & FLAG_FRAME_TYPE)) | |
1225 break; | |
1226 if(get_v(bc) != size) | |
1227 break; | |
1228 pos -= size; | |
1229 size= get_v(bc); | |
1230 av_log(s, AV_LOG_DEBUG, "steping back to %lld next %d\n", pos, size); | |
1231 } | |
1232 url_fseek(bc, pos, SEEK_SET); | |
1233 | |
426 | 1234 nut->written_packet_size= -1; |
1235 } | |
1236 } | |
1237 } | |
1238 | |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
432
diff
changeset
|
1239 static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index, int64_t *pos_arg, int64_t pos_limit){ |
426 | 1240 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
|
1241 StreamContext *stream; |
426 | 1242 ByteIOContext *bc = &s->pb; |
1243 int64_t pos, pts; | |
1244 uint64_t code; | |
1245 int frame_code,step, flags, stream_id, i; | |
1246 av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%lld,%lld)\n", stream_index, *pos_arg, pos_limit); | |
1247 | |
1248 if(*pos_arg < 0) | |
1249 return AV_NOPTS_VALUE; | |
1250 | |
1251 // find a previous startcode, FIXME use forward search and follow backward pointers if undamaged stream | |
1252 pos= *pos_arg; | |
1253 step= FFMIN(16*1024, pos); | |
1254 do{ | |
1255 pos-= step; | |
1256 code= find_any_startcode(bc, pos); | |
1257 | |
1258 if(code && url_ftell(bc) - 8 < *pos_arg) | |
1259 break; | |
1260 step= FFMIN(2*step, pos); | |
1261 }while(step); | |
1262 | |
1263 if(!code) //nothing found, not even after pos_arg | |
1264 return AV_NOPTS_VALUE; | |
1265 | |
1266 url_fseek(bc, -8, SEEK_CUR); | |
1267 for(i=0; i<s->nb_streams; i++) | |
1268 nut->stream[i].last_sync_pos= url_ftell(bc); | |
1269 | |
1270 for(;;){ | |
1271 int64_t pos= url_ftell(bc); | |
1272 uint64_t tmp=0; | |
1273 int prefix_len=1; | |
1274 | |
1275 if(pos > pos_limit) | |
1276 return AV_NOPTS_VALUE; | |
1277 | |
1278 frame_code = get_byte(bc); | |
1279 if(frame_code == 'N'){ | |
1280 tmp= frame_code; | |
1281 for(i=1; i<8; i++) | |
1282 tmp = (tmp<<8) + get_byte(bc); | |
1283 } | |
1284 //av_log(s, AV_LOG_DEBUG, "before switch %llX at=%lld\n", tmp, pos); | |
1285 | |
1286 switch(tmp){ | |
1287 case MAIN_STARTCODE: | |
1288 case STREAM_STARTCODE: | |
1289 case INDEX_STARTCODE: | |
1290 case INFO_STARTCODE: | |
1291 nut->written_packet_size= -1; | |
1292 get_packetheader(nut, bc, 8, 0); | |
1293 url_fseek(bc, nut->written_packet_size + nut->packet_start, SEEK_SET); | |
1294 break; | |
1295 case KEYFRAME_STARTCODE: | |
1296 nut->written_packet_size= -1; | |
1297 prefix_len+=8; | |
1298 frame_code = get_byte(bc); | |
1299 case 0: | |
1300 flags= nut->frame_code[frame_code].flags; | |
1301 stream_id= nut->frame_code[frame_code].stream_id_plus1 - 1; | |
1302 | |
1303 if(get_packetheader(nut, bc, prefix_len, 0) < 0) | |
1304 goto resync; | |
1305 | |
1306 if(!(flags & FLAG_FRAME_TYPE) || !(flags & FLAG_PTS) || !(flags & FLAG_FULL_PTS)) | |
1307 goto resync; | |
1308 | |
1309 if(stream_id==-1) | |
1310 stream_id= get_v(bc); | |
1311 if(stream_id >= s->nb_streams) | |
1312 goto resync; | |
1313 | |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
432
diff
changeset
|
1314 stream= &nut->stream[stream_id]; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
432
diff
changeset
|
1315 pts= get_v(bc) * AV_TIME_BASE * stream->rate_den / stream->rate_num; |
426 | 1316 |
1317 if(flags & FLAG_KEY_FRAME){ | |
1318 av_add_index_entry( | |
1319 s->streams[stream_id], | |
1320 pos, | |
1321 pts, | |
1322 pos - nut->stream[stream_id].last_sync_pos, | |
1323 AVINDEX_KEYFRAME); | |
1324 nut->stream[stream_id].last_sync_pos= pos; | |
1325 } | |
1326 if(stream_id != stream_index || !(flags & FLAG_KEY_FRAME) || nut->packet_start < *pos_arg){ | |
1327 url_fseek(bc, nut->written_packet_size + nut->packet_start, SEEK_SET); | |
1328 break; | |
1329 } | |
1330 | |
1331 *pos_arg= nut->packet_start; | |
1332 assert(nut->packet_start == pos); | |
1333 return pts; | |
1334 default: | |
1335 resync: | |
1336 av_log(s, AV_LOG_DEBUG, "syncing from %lld\n", nut->packet_start+1); | |
1337 if(!find_any_startcode(bc, nut->packet_start+1)) | |
1338 return AV_NOPTS_VALUE; | |
1339 | |
1340 url_fseek(bc, -8, SEEK_CUR); | |
421 | 1341 } |
1342 } | |
426 | 1343 return AV_NOPTS_VALUE; |
1344 } | |
1345 | |
1346 static int nut_read_seek(AVFormatContext *s, int stream_index, int64_t target_ts){ | |
1347 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
|
1348 int64_t pos; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
432
diff
changeset
|
1349 int i; |
426 | 1350 |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
432
diff
changeset
|
1351 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
|
1352 return -1; |
426 | 1353 |
1354 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
|
1355 pos= url_ftell(&s->pb); |
426 | 1356 for(i=0; i<s->nb_streams; i++) |
1357 nut->stream[i].last_sync_pos= pos; | |
1358 | |
1359 return 0; | |
403 | 1360 } |
1361 | |
1362 static int nut_read_close(AVFormatContext *s) | |
1363 { | |
1364 NUTContext *nut = s->priv_data; | |
1365 int i; | |
1366 | |
1367 for(i=0;i<s->nb_streams;i++) { | |
1368 av_freep(&s->streams[i]->codec.extradata); | |
1369 } | |
1370 av_freep(&nut->stream); | |
219 | 1371 |
1372 return 0; | |
1373 } | |
1374 | |
1375 static AVInputFormat nut_iformat = { | |
1376 "nut", | |
1377 "nut format", | |
1378 sizeof(NUTContext), | |
1379 nut_probe, | |
1380 nut_read_header, | |
1381 nut_read_packet, | |
403 | 1382 nut_read_close, |
426 | 1383 nut_read_seek, |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
432
diff
changeset
|
1384 nut_read_timestamp, |
219 | 1385 .extensions = "nut", |
1386 }; | |
1387 | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
272
diff
changeset
|
1388 #ifdef CONFIG_ENCODERS |
219 | 1389 static AVOutputFormat nut_oformat = { |
1390 "nut", | |
1391 "nut format", | |
1392 "video/x-nut", | |
1393 "nut", | |
1394 sizeof(NUTContext), | |
414 | 1395 #ifdef CONFIG_VORBIS |
219 | 1396 CODEC_ID_VORBIS, |
1397 #elif defined(CONFIG_MP3LAME) | |
232 | 1398 CODEC_ID_MP3, |
219 | 1399 #else |
223 | 1400 CODEC_ID_MP2, /* AC3 needs liba52 decoder */ |
219 | 1401 #endif |
1402 CODEC_ID_MPEG4, | |
1403 nut_write_header, | |
1404 nut_write_packet, | |
1405 nut_write_trailer, | |
1406 }; | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
272
diff
changeset
|
1407 #endif //CONFIG_ENCODERS |
219 | 1408 |
1409 int nut_init(void) | |
1410 { | |
1411 av_register_input_format(&nut_iformat); | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
272
diff
changeset
|
1412 #ifdef CONFIG_ENCODERS |
219 | 1413 av_register_output_format(&nut_oformat); |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
272
diff
changeset
|
1414 #endif //CONFIG_ENCODERS |
219 | 1415 return 0; |
1416 } |