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