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