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