Mercurial > libavformat.hg
annotate nut.c @ 416:e26aacf263be libavformat
10l (Titel->Title)
| author | michael |
|---|---|
| date | Mon, 05 Apr 2004 10:38:01 +0000 |
| parents | badb7a946a9c |
| children | 41da3366d341 |
| 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 * - checksumming | |
| 403 | 29 * - seeking |
| 219 | 30 * - index writing |
| 415 | 31 * - index packet reading support |
| 219 | 32 * - startcode searching for broken streams |
| 33 */ | |
| 34 | |
| 35 //#define DEBUG 1 | |
| 36 | |
| 403 | 37 #include <limits.h> |
| 219 | 38 #include "avformat.h" |
| 39 #include "mpegaudio.h" | |
| 228 | 40 #include "avi.h" |
| 219 | 41 |
| 403 | 42 #undef NDEBUG |
| 43 #include <assert.h> | |
| 44 | |
| 220 | 45 //from /dev/random |
| 46 | |
| 403 | 47 #define MAIN_STARTCODE (0x7A561F5F04ADULL + (((uint64_t)('N'<<8) + 'M')<<48)) |
| 48 #define STREAM_STARTCODE (0x11405BF2F9DBULL + (((uint64_t)('N'<<8) + 'S')<<48)) | |
| 49 #define KEYFRAME_STARTCODE (0xE4ADEECA4569ULL + (((uint64_t)('N'<<8) + 'K')<<48)) | |
| 50 #define INDEX_STARTCODE (0xDD672F23E64EULL + (((uint64_t)('N'<<8) + 'X')<<48)) | |
| 51 #define INFO_STARTCODE (0xAB68B596BA78ULL + (((uint64_t)('N'<<8) + 'I')<<48)) | |
| 52 | |
| 53 #define MAX_TYPE1_DISTANCE (1024*16-1) | |
| 54 #define MAX_TYPE2_DISTANCE (1024*64-1) | |
| 55 | |
| 56 #define MAX_SIZE_LRU 2 | |
| 57 #define MAX_PTS_LRU 3 | |
| 58 | |
| 59 #define FLAG_FRAME_TYPE 1 | |
| 60 #define FLAG_DATA_SIZE 2 | |
| 61 #define FLAG_PTS 16 | |
| 62 #define FLAG_FULL_PTS 4 | |
| 63 #define FLAG_KEY_FRAME 32 | |
| 64 #define FLAG_PRED_KEY_FRAME 64 | |
| 65 | |
| 66 typedef struct { | |
| 67 uint8_t flags; | |
| 68 uint8_t stream_id_plus1; | |
| 69 uint8_t size_mul; | |
| 70 uint8_t size_lsb; | |
| 71 } FrameCode; | |
| 72 | |
| 73 typedef struct { | |
| 74 int last_key_frame; | |
| 75 int msb_timestamp_shift; | |
| 76 int rate_num; | |
| 77 int rate_den; | |
| 78 int64_t last_pts; | |
| 79 int64_t last_full_pts; | |
| 80 int lru_pts_delta[MAX_PTS_LRU]; | |
| 81 int lru_size[MAX_SIZE_LRU]; | |
| 82 int initial_pts_predictor[MAX_PTS_LRU]; | |
| 83 int initial_size_predictor[MAX_SIZE_LRU]; | |
| 84 } StreamContext; | |
| 220 | 85 |
| 219 | 86 typedef struct { |
| 403 | 87 AVFormatContext *avf; |
| 88 int64_t packet_start; | |
| 89 int64_t last_packet_start; | |
| 90 int written_packet_size; | |
| 91 int64_t packet_size_pos; | |
| 92 int64_t last_frame_start[3]; | |
| 93 FrameCode frame_code[256]; | |
| 94 StreamContext *stream; | |
| 219 | 95 } NUTContext; |
| 96 | |
| 415 | 97 static char *info_table[][2]={ |
| 98 {NULL , NULL }, // end | |
| 99 {NULL , NULL }, | |
| 100 {NULL , "UTF8"}, | |
| 101 {NULL , "v"}, | |
| 102 {NULL , "s"}, | |
| 103 {"StreamId" , "v"}, | |
| 104 {"SegmentId" , "v"}, | |
| 105 {"StartTimestamp" , "v"}, | |
| 106 {"EndTimestamp" , "v"}, | |
| 107 {"Author" , "UTF8"}, | |
| 416 | 108 {"Title" , "UTF8"}, |
| 415 | 109 {"Description" , "UTF8"}, |
| 110 {"Copyright" , "UTF8"}, | |
| 111 {"Encoder" , "UTF8"}, | |
| 112 {"Keyword" , "UTF8"}, | |
| 113 {"Cover" , "JPEG"}, | |
| 114 {"Cover" , "PNG"}, | |
| 115 }; | |
| 116 | |
| 403 | 117 static void update_lru(int *lru, int current, int count){ |
| 118 int i; | |
| 119 | |
| 120 for(i=0; i<count-1; i++){ | |
| 121 if(lru[i] == current) | |
| 122 break; | |
| 123 } | |
| 124 | |
| 125 for(; i; i--){ | |
| 126 lru[i]= lru[i-1]; | |
| 127 } | |
| 128 | |
| 129 lru[0]= current; | |
| 130 } | |
| 131 | |
| 132 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){ | |
| 133 StreamContext *stream= &nut->stream[stream_index]; | |
| 134 | |
| 135 stream->last_key_frame= key_frame; | |
| 136 nut->last_frame_start[ frame_type ]= frame_start; | |
| 137 update_lru(stream->lru_pts_delta, pts - stream->last_pts, 3); | |
| 138 update_lru(stream->lru_size , size, 2); | |
| 139 stream->last_pts= pts; | |
| 140 if( nut->frame_code[frame_code].flags & FLAG_PTS | |
| 141 && nut->frame_code[frame_code].flags & FLAG_FULL_PTS) | |
| 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 int bytes_left(ByteIOContext *bc) |
| 248 { | |
| 249 return bc->buf_end - bc->buf_ptr; | |
| 250 } | |
| 251 | |
| 252 static uint64_t get_v(ByteIOContext *bc) | |
| 253 { | |
| 254 uint64_t val = 0; | |
| 255 | |
| 256 for(; bytes_left(bc) > 0; ) | |
| 257 { | |
| 258 int tmp = get_byte(bc); | |
| 259 | |
| 260 if (tmp&0x80) | |
| 261 val= (val<<7) + tmp - 0x80; | |
| 262 else | |
| 263 return (val<<7) + tmp; | |
| 264 } | |
| 265 return -1; | |
| 266 } | |
| 267 | |
| 415 | 268 static int get_str(ByteIOContext *bc, char *string, int maxlen){ |
| 269 int len= get_v(bc); | |
| 219 | 270 |
| 415 | 271 if(len && maxlen) |
| 272 get_buffer(bc, string, FFMIN(len, maxlen)); | |
| 273 while(len > maxlen){ | |
| 274 get_byte(bc); | |
| 275 len--; | |
| 276 } | |
| 240 | 277 |
| 415 | 278 if(maxlen) |
| 279 string[FFMIN(len, maxlen-1)]= 0; | |
| 240 | 280 |
| 415 | 281 if(maxlen == len) |
| 282 return -1; | |
| 283 else | |
| 284 return 0; | |
| 219 | 285 } |
| 286 | |
| 403 | 287 static int get_packetheader(NUTContext *nut, ByteIOContext *bc, int prefix_length) |
| 219 | 288 { |
| 403 | 289 int64_t start, size, last_size; |
| 290 start= url_ftell(bc) - prefix_length; | |
| 291 | |
| 292 if(start != nut->packet_start + nut->written_packet_size){ | |
| 293 av_log(nut->avf, AV_LOG_ERROR, "get_packetheader called at weird position\n"); | |
| 294 return -1; | |
| 295 } | |
| 296 | |
| 297 size= get_v(bc); | |
| 298 last_size= get_v(bc); | |
| 299 if(nut->written_packet_size != last_size){ | |
| 300 av_log(nut->avf, AV_LOG_ERROR, "packet size missmatch %d != %lld at %lld\n", nut->written_packet_size, last_size, start); | |
| 301 return -1; | |
| 302 } | |
| 303 | |
| 304 nut->last_packet_start = nut->packet_start; | |
| 305 nut->packet_start = start; | |
| 306 nut->written_packet_size= size; | |
| 307 | |
| 308 return size; | |
| 219 | 309 } |
| 310 | |
| 221 | 311 /** |
| 312 * | |
| 313 */ | |
| 314 static int get_length(uint64_t val){ | |
| 315 int i; | |
| 219 | 316 |
| 221 | 317 for (i=7; ; i+=7) |
| 318 if ((val>>i) == 0) | |
| 319 return i; | |
| 219 | 320 |
| 221 | 321 return 7; //not reached |
| 219 | 322 } |
| 323 | |
|
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
272
diff
changeset
|
324 #ifdef CONFIG_ENCODERS |
| 219 | 325 static int put_v(ByteIOContext *bc, uint64_t val) |
| 326 { | |
| 327 int i; | |
| 328 // if (bytes_left(s)*8 < 9) | |
| 329 // return -1; | |
| 330 | |
| 331 if (bytes_left(bc) < 1) | |
| 332 return -1; | |
| 333 | |
| 334 val &= 0x7FFFFFFFFFFFFFFFULL; // FIXME can only encode upto 63 bits currently | |
| 221 | 335 i= get_length(val); |
| 219 | 336 |
| 220 | 337 for (i-=7; i>0; i-=7){ |
| 219 | 338 put_byte(bc, 0x80 | (val>>i)); |
| 220 | 339 } |
| 219 | 340 |
| 341 put_byte(bc, val&0x7f); | |
| 342 | |
| 343 return 0; | |
| 344 } | |
| 345 | |
| 415 | 346 static int put_str(ByteIOContext *bc, const char *string){ |
| 347 int len= strlen(string); | |
| 219 | 348 |
| 349 put_v(bc, len); | |
| 415 | 350 put_buffer(bc, string, len); |
| 351 | |
| 228 | 352 return 0; |
| 353 } | |
| 354 | |
| 221 | 355 static int put_packetheader(NUTContext *nut, ByteIOContext *bc, int max_size) |
| 219 | 356 { |
| 357 put_flush_packet(bc); | |
| 403 | 358 nut->last_packet_start= nut->packet_start; |
| 359 nut->packet_start+= nut->written_packet_size; | |
| 360 nut->packet_size_pos = url_ftell(bc); | |
| 361 nut->written_packet_size = max_size; | |
| 362 | |
| 219 | 363 /* packet header */ |
| 403 | 364 put_v(bc, nut->written_packet_size); /* forward ptr */ |
| 365 put_v(bc, nut->packet_start - nut->last_packet_start); /* backward ptr */ | |
| 219 | 366 |
| 367 return 0; | |
| 368 } | |
| 369 | |
| 221 | 370 static int update_packetheader(NUTContext *nut, ByteIOContext *bc, int additional_size){ |
| 403 | 371 int64_t start= nut->packet_start; |
| 372 int64_t cur= url_ftell(bc); | |
| 221 | 373 int size= cur - start + additional_size; |
| 374 | |
| 403 | 375 if(size != nut->written_packet_size){ |
| 376 int i; | |
| 377 | |
| 378 assert( size <= nut->written_packet_size ); | |
| 219 | 379 |
| 403 | 380 url_fseek(bc, nut->packet_size_pos, SEEK_SET); |
| 381 for(i=get_length(size); i < get_length(nut->written_packet_size); i+=7) | |
| 382 put_byte(bc, 0x80); | |
| 383 put_v(bc, size); | |
| 219 | 384 |
| 403 | 385 url_fseek(bc, cur, SEEK_SET); |
| 386 nut->written_packet_size= size; //FIXME may fail if multiple updates with differing sizes, as get_length may differ | |
| 387 } | |
| 221 | 388 |
| 219 | 389 return 0; |
| 390 } | |
| 391 | |
| 392 static int nut_write_header(AVFormatContext *s) | |
| 393 { | |
| 394 NUTContext *nut = s->priv_data; | |
| 395 ByteIOContext *bc = &s->pb; | |
| 396 AVCodecContext *codec; | |
| 403 | 397 int i, j; |
| 219 | 398 |
| 403 | 399 nut->avf= s; |
| 400 | |
| 401 nut->stream = | |
| 402 av_mallocz(sizeof(StreamContext)*s->nb_streams); | |
| 403 | |
| 404 av_set_pts_info(s, 60, 1, AV_TIME_BASE); | |
| 405 | |
| 219 | 406 /* main header */ |
| 220 | 407 put_be64(bc, MAIN_STARTCODE); |
| 403 | 408 put_packetheader(nut, bc, 120+5*256); |
| 409 put_v(bc, 1); /* version */ | |
| 219 | 410 put_v(bc, s->nb_streams); |
| 403 | 411 put_v(bc, 3); /* checksum threshold */ |
| 412 | |
| 413 build_frame_code(s); | |
| 414 assert(nut->frame_code['N'].flags == 1); | |
| 415 for(i=0; i<256;){ | |
| 416 int tmp_flags = nut->frame_code[i].flags; | |
| 417 int tmp_stream= nut->frame_code[i].stream_id_plus1; | |
| 418 int tmp_mul = nut->frame_code[i].size_mul; | |
| 419 int tmp_size = nut->frame_code[i].size_lsb; | |
| 420 put_v(bc, tmp_flags); | |
| 421 put_v(bc, tmp_stream); | |
| 422 put_v(bc, tmp_mul); | |
| 423 put_v(bc, tmp_size); | |
| 424 | |
| 425 for(j=0; i<256; j++,i++){ | |
| 426 if(nut->frame_code[i].flags != tmp_flags ) break; | |
| 427 if(nut->frame_code[i].stream_id_plus1 != tmp_stream) break; | |
| 428 if(nut->frame_code[i].size_mul != tmp_mul ) break; | |
| 429 if(nut->frame_code[i].size_lsb != tmp_size ) break; | |
| 430 if(++tmp_size >= tmp_mul){ | |
| 431 tmp_size=0; | |
| 432 tmp_stream++; | |
| 433 } | |
| 434 } | |
| 435 put_v(bc, j); | |
| 436 } | |
| 437 | |
| 220 | 438 put_be32(bc, 0); /* FIXME: checksum */ |
| 219 | 439 |
| 221 | 440 update_packetheader(nut, bc, 0); |
| 441 | |
| 219 | 442 /* stream headers */ |
| 443 for (i = 0; i < s->nb_streams; i++) | |
| 444 { | |
| 403 | 445 int nom, denom, gcd; |
| 271 | 446 |
| 219 | 447 codec = &s->streams[i]->codec; |
| 448 | |
| 223 | 449 put_be64(bc, STREAM_STARTCODE); |
| 410 | 450 put_packetheader(nut, bc, 120 + codec->extradata_size); |
| 222 | 451 put_v(bc, i /*s->streams[i]->index*/); |
| 219 | 452 put_v(bc, (codec->codec_type == CODEC_TYPE_AUDIO) ? 32 : 0); |
| 453 if (codec->codec_tag) | |
| 415 | 454 put_v(bc, codec->codec_tag); |
| 219 | 455 else if (codec->codec_type == CODEC_TYPE_VIDEO) |
| 456 { | |
| 415 | 457 put_v(bc, codec_get_bmp_tag(codec->codec_id)); |
| 219 | 458 } |
| 459 else if (codec->codec_type == CODEC_TYPE_AUDIO) | |
| 460 { | |
| 415 | 461 put_v(bc, codec_get_wav_tag(codec->codec_id)); |
| 282 | 462 } |
| 415 | 463 else |
| 464 put_v(bc, 0); | |
| 282 | 465 |
| 466 if (codec->codec_type == CODEC_TYPE_VIDEO) | |
| 467 { | |
| 468 nom = codec->frame_rate; | |
| 469 denom = codec->frame_rate_base; | |
| 470 } | |
| 471 else | |
| 472 { | |
| 403 | 473 nom = codec->sample_rate; |
| 474 if(codec->frame_size>0) | |
| 475 denom= codec->frame_size; | |
| 476 else | |
| 477 denom= 1; //unlucky | |
| 219 | 478 } |
| 403 | 479 gcd= ff_gcd(nom, denom); |
| 480 nom /= gcd; | |
| 481 denom /= gcd; | |
| 482 nut->stream[i].rate_num= nom; | |
| 483 nut->stream[i].rate_den= denom; | |
| 484 | |
| 219 | 485 put_v(bc, codec->bit_rate); |
| 486 put_v(bc, 0); /* no language code */ | |
| 271 | 487 put_v(bc, nom); |
| 488 put_v(bc, denom); | |
| 403 | 489 if(nom / denom < 1000) |
| 490 nut->stream[i].msb_timestamp_shift = 7; | |
| 491 else | |
| 492 nut->stream[i].msb_timestamp_shift = 14; | |
| 493 put_v(bc, nut->stream[i].msb_timestamp_shift); | |
| 494 for(j=0; j<3; j++) | |
| 495 put_v(bc, nut->stream[i].initial_pts_predictor[j]); | |
| 496 for(j=0; j<2; j++) | |
| 497 put_v(bc, nut->stream[i].initial_size_predictor[j]); | |
| 219 | 498 put_byte(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */ |
| 499 | |
| 403 | 500 if(codec->extradata_size){ |
| 501 put_v(bc, 1); | |
| 502 put_v(bc, codec->extradata_size); | |
| 503 put_buffer(bc, codec->extradata, codec->extradata_size); | |
| 504 } | |
| 505 put_v(bc, 0); /* end of codec specific headers */ | |
| 219 | 506 |
| 507 switch(codec->codec_type) | |
| 508 { | |
| 509 case CODEC_TYPE_AUDIO: | |
| 271 | 510 put_v(bc, (codec->sample_rate * denom) / nom); |
| 219 | 511 put_v(bc, codec->channels); |
| 512 break; | |
| 513 case CODEC_TYPE_VIDEO: | |
| 514 put_v(bc, codec->width); | |
| 515 put_v(bc, codec->height); | |
| 403 | 516 put_v(bc, codec->sample_aspect_ratio.num); |
| 517 put_v(bc, codec->sample_aspect_ratio.den); | |
| 219 | 518 put_v(bc, 0); /* csp type -- unknown */ |
| 519 break; | |
| 228 | 520 default: |
| 521 break; | |
| 219 | 522 } |
| 403 | 523 put_be32(bc, 0); /* FIXME: checksum */ |
| 221 | 524 update_packetheader(nut, bc, 0); |
| 219 | 525 } |
| 526 | |
| 527 /* info header */ | |
| 223 | 528 put_be64(bc, INFO_STARTCODE); |
| 415 | 529 put_packetheader(nut, bc, 30+strlen(s->author)+strlen(s->title)+ |
| 530 strlen(s->comment)+strlen(s->copyright)+strlen(LIBAVFORMAT_IDENT)); | |
| 219 | 531 if (s->author[0]) |
| 532 { | |
| 415 | 533 put_v(bc, 9); /* type */ |
| 534 put_str(bc, s->author); | |
| 219 | 535 } |
| 536 if (s->title[0]) | |
| 537 { | |
| 415 | 538 put_v(bc, 10); /* type */ |
| 539 put_str(bc, s->title); | |
| 219 | 540 } |
| 541 if (s->comment[0]) | |
| 542 { | |
| 415 | 543 put_v(bc, 11); /* type */ |
| 544 put_str(bc, s->comment); | |
| 219 | 545 } |
| 546 if (s->copyright[0]) | |
| 547 { | |
| 415 | 548 put_v(bc, 12); /* type */ |
| 549 put_str(bc, s->copyright); | |
| 219 | 550 } |
| 551 /* encoder */ | |
| 415 | 552 put_v(bc, 13); /* type */ |
| 553 put_str(bc, LIBAVFORMAT_IDENT); | |
| 222 | 554 |
| 555 put_v(bc, 0); /* eof info */ | |
| 219 | 556 |
| 220 | 557 put_be32(bc, 0); /* FIXME: checksum */ |
| 221 | 558 update_packetheader(nut, bc, 0); |
| 219 | 559 |
| 560 put_flush_packet(bc); | |
| 561 | |
| 562 return 0; | |
| 563 } | |
| 564 | |
| 565 static int nut_write_packet(AVFormatContext *s, int stream_index, | |
| 241 | 566 const uint8_t *buf, int size, int64_t pts) |
| 219 | 567 { |
| 568 NUTContext *nut = s->priv_data; | |
| 403 | 569 StreamContext *stream= &nut->stream[stream_index]; |
| 219 | 570 ByteIOContext *bc = &s->pb; |
| 403 | 571 int key_frame = 0, full_pts=0; |
| 219 | 572 AVCodecContext *enc; |
| 403 | 573 int64_t lsb_pts, delta_pts; |
| 574 int frame_type, best_length, frame_code, flags, i, size_mul, size_lsb; | |
| 575 const int64_t frame_start= url_ftell(bc); | |
| 219 | 576 |
| 577 if (stream_index > s->nb_streams) | |
| 578 return 1; | |
| 403 | 579 |
| 580 pts= (av_rescale(pts, stream->rate_num, stream->rate_den) + AV_TIME_BASE/2) / AV_TIME_BASE; | |
| 219 | 581 |
| 582 enc = &s->streams[stream_index]->codec; | |
| 403 | 583 key_frame = enc->coded_frame->key_frame; |
| 584 delta_pts= pts - stream->last_pts; | |
| 585 | |
| 586 frame_type=0; | |
| 587 if(frame_start + size + 20 - FFMAX(nut->last_frame_start[1], nut->last_frame_start[2]) > MAX_TYPE1_DISTANCE) | |
| 588 frame_type=1; | |
| 589 if(key_frame){ | |
| 590 if(frame_type==1 && frame_start + size - nut->last_frame_start[2] > MAX_TYPE2_DISTANCE) | |
| 591 frame_type=2; | |
| 592 if(!stream->last_key_frame) | |
| 593 frame_type=2; | |
| 328 | 594 } |
| 595 | |
| 403 | 596 if(frame_type>0){ |
| 597 update_packetheader(nut, bc, 0); | |
| 598 reset(s); | |
| 599 full_pts=1; | |
| 600 } | |
| 601 //FIXME ensure that the timestamp can be represented by either delta or lsb or full_pts=1 | |
| 602 | |
| 603 lsb_pts = pts & ((1 << stream->msb_timestamp_shift)-1); | |
| 604 | |
| 605 best_length=INT_MAX; | |
| 606 frame_code= -1; | |
| 607 for(i=0; i<256; i++){ | |
| 608 int stream_id_plus1= nut->frame_code[i].stream_id_plus1; | |
| 609 int fc_key_frame= stream->last_key_frame; | |
| 610 int length=0; | |
| 611 size_mul= nut->frame_code[i].size_mul; | |
| 612 size_lsb= nut->frame_code[i].size_lsb; | |
| 613 flags= nut->frame_code[i].flags; | |
| 614 | |
| 615 if(stream_id_plus1 == 0) length+= get_length(stream_index); | |
| 616 else if(stream_id_plus1 - 1 != stream_index) | |
| 617 continue; | |
| 618 if(flags & FLAG_PRED_KEY_FRAME){ | |
| 619 if(flags & FLAG_KEY_FRAME) | |
| 620 fc_key_frame= !fc_key_frame; | |
| 621 }else{ | |
| 622 fc_key_frame= !!(flags & FLAG_KEY_FRAME); | |
| 623 } | |
| 624 assert(key_frame==0 || key_frame==1); | |
| 625 if(fc_key_frame != key_frame) | |
| 626 continue; | |
| 627 | |
| 628 if((!!(flags & FLAG_FRAME_TYPE)) != (frame_type > 0)) | |
| 629 continue; | |
| 630 | |
| 631 if(size_mul <= size_lsb){ | |
| 632 int p= stream->lru_size[size_lsb - size_mul]; | |
| 633 if(p != size) | |
| 634 continue; | |
| 635 }else{ | |
| 636 if(size % size_mul != size_lsb) | |
| 637 continue; | |
| 638 if(flags & FLAG_DATA_SIZE) | |
| 639 length += get_length(size / size_mul); | |
| 640 else if(size/size_mul) | |
| 641 continue; | |
| 642 } | |
| 220 | 643 |
| 403 | 644 if(full_pts != ((flags & FLAG_PTS) && (flags & FLAG_FULL_PTS))) |
| 645 continue; | |
| 646 | |
| 647 if(flags&FLAG_PTS){ | |
| 648 if(flags&FLAG_FULL_PTS){ | |
| 649 length += get_length(pts); | |
| 650 }else{ | |
| 651 length += get_length(lsb_pts); | |
| 652 } | |
| 653 }else{ | |
| 654 int delta= stream->lru_pts_delta[(flags & 12)>>2]; | |
| 655 if(delta != pts - stream->last_pts) | |
| 656 continue; | |
| 657 assert(frame_type == 0); | |
| 658 } | |
| 659 | |
| 660 if(length < best_length){ | |
| 661 best_length= length; | |
| 662 frame_code=i; | |
| 663 } | |
| 664 // 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); | |
| 665 } | |
| 223 | 666 |
| 403 | 667 assert(frame_code != -1); |
| 668 flags= nut->frame_code[frame_code].flags; | |
| 669 size_mul= nut->frame_code[frame_code].size_mul; | |
| 670 size_lsb= nut->frame_code[frame_code].size_lsb; | |
| 671 #if 0 | |
| 672 best_length /= 7; | |
| 673 best_length ++; //frame_code | |
| 674 if(frame_type>0){ | |
| 675 best_length += 4; //packet header | |
| 676 if(frame_type>1) | |
| 677 best_length += 8; // startcode | |
| 678 } | |
| 679 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)); | |
| 680 #endif | |
| 681 | |
| 682 if (frame_type==2) | |
| 683 put_be64(bc, KEYFRAME_STARTCODE); | |
| 684 put_byte(bc, frame_code); | |
| 685 | |
| 686 if(frame_type>0) | |
| 687 put_packetheader(nut, bc, FFMAX(size+20, MAX_TYPE1_DISTANCE)); | |
| 688 if(nut->frame_code[frame_code].stream_id_plus1 == 0) | |
| 689 put_v(bc, stream_index); | |
| 690 if (flags & FLAG_PTS){ | |
| 691 if (flags & FLAG_FULL_PTS) | |
| 692 put_v(bc, pts); | |
| 693 else | |
| 694 put_v(bc, lsb_pts); | |
| 695 } | |
| 696 if(flags & FLAG_DATA_SIZE) | |
| 697 put_v(bc, size / size_mul); | |
| 698 if(size > MAX_TYPE1_DISTANCE){ | |
| 699 assert(frame_type > 0); | |
| 700 update_packetheader(nut, bc, size); | |
| 701 } | |
| 219 | 702 |
| 703 put_buffer(bc, buf, size); | |
| 403 | 704 |
| 705 update(nut, stream_index, frame_start, frame_type, frame_code, key_frame, size, pts); | |
| 219 | 706 |
| 707 return 0; | |
| 708 } | |
| 709 | |
| 710 static int nut_write_trailer(AVFormatContext *s) | |
| 711 { | |
| 328 | 712 NUTContext *nut = s->priv_data; |
| 219 | 713 ByteIOContext *bc = &s->pb; |
| 403 | 714 |
| 715 update_packetheader(nut, bc, 0); | |
| 716 | |
| 219 | 717 #if 0 |
| 718 int i; | |
| 719 | |
| 720 /* WRITE INDEX */ | |
| 721 | |
| 722 for (i = 0; s->nb_streams; i++) | |
| 723 { | |
| 223 | 724 put_be64(bc, INDEX_STARTCODE); |
| 221 | 725 put_packetheader(nut, bc, 64); |
| 219 | 726 put_v(bc, s->streams[i]->id); |
| 727 put_v(bc, ...); | |
| 220 | 728 put_be32(bc, 0); /* FIXME: checksum */ |
| 221 | 729 update_packetheader(nut, bc, 0); |
| 219 | 730 } |
| 731 #endif | |
| 732 | |
| 733 put_flush_packet(bc); | |
| 328 | 734 |
| 403 | 735 av_freep(&nut->stream); |
| 219 | 736 |
| 737 return 0; | |
| 738 } | |
|
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
272
diff
changeset
|
739 #endif //CONFIG_ENCODERS |
| 219 | 740 |
| 741 static int nut_probe(AVProbeData *p) | |
| 742 { | |
| 220 | 743 int i; |
| 744 uint64_t code; | |
| 745 | |
| 746 code = 0xff; | |
| 747 for (i = 0; i < p->buf_size; i++) { | |
| 748 int c = p->buf[i]; | |
| 749 code = (code << 8) | c; | |
| 750 if (code == MAIN_STARTCODE) | |
| 751 return AVPROBE_SCORE_MAX; | |
| 752 } | |
| 753 return 0; | |
| 219 | 754 } |
| 755 | |
| 756 static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap) | |
| 757 { | |
| 758 NUTContext *nut = s->priv_data; | |
| 759 ByteIOContext *bc = &s->pb; | |
| 220 | 760 uint64_t tmp; |
| 403 | 761 int cur_stream, nb_streams, i, j; |
| 762 | |
| 763 nut->avf= s; | |
| 219 | 764 |
| 403 | 765 av_set_pts_info(s, 60, 1, AV_TIME_BASE); |
| 766 | |
| 219 | 767 /* main header */ |
| 220 | 768 tmp = get_be64(bc); |
| 769 if (tmp != MAIN_STARTCODE) | |
|
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
328
diff
changeset
|
770 av_log(s, AV_LOG_ERROR, "damaged? startcode!=1 (%Ld)\n", tmp); |
| 403 | 771 get_packetheader(nut, bc, 8); |
| 219 | 772 |
| 773 tmp = get_v(bc); | |
| 403 | 774 if (tmp != 1) |
|
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
328
diff
changeset
|
775 av_log(s, AV_LOG_ERROR, "bad version (%Ld)\n", tmp); |
| 219 | 776 |
| 777 nb_streams = get_v(bc); | |
| 403 | 778 get_v(bc); //checksum threshold |
| 779 | |
| 780 for(i=0; i<256;){ | |
| 781 int tmp_flags = get_v(bc); | |
| 782 int tmp_stream= get_v(bc); | |
| 783 int tmp_mul = get_v(bc); | |
| 784 int tmp_size = get_v(bc); | |
| 785 int count = get_v(bc); | |
| 786 | |
| 787 if(count == 0 || i+count > 256){ | |
| 788 av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i); | |
| 789 return -1; | |
| 790 } | |
| 791 | |
| 792 if((tmp_flags & FLAG_FRAME_TYPE) && tmp_flags != 1){ | |
| 793 if(tmp_flags & FLAG_PRED_KEY_FRAME){ | |
| 794 av_log(s, AV_LOG_ERROR, "keyframe prediction in non 0 frame type\n"); | |
| 795 return -1; | |
| 796 } | |
| 797 if(!(tmp_flags & FLAG_PTS) || !(tmp_flags & FLAG_FULL_PTS) ){ | |
| 798 av_log(s, AV_LOG_ERROR, "no full pts in non 0 frame type\n"); | |
| 799 return -1; | |
| 800 } | |
| 801 } | |
| 802 | |
| 803 for(j=0; j<count; j++,i++){ | |
| 804 if(tmp_stream > nb_streams + 1){ | |
| 805 av_log(s, AV_LOG_ERROR, "illegal stream number\n"); | |
| 806 return -1; | |
| 807 } | |
| 808 | |
| 809 nut->frame_code[i].flags = tmp_flags ; | |
| 810 nut->frame_code[i].stream_id_plus1 = tmp_stream; | |
| 811 nut->frame_code[i].size_mul = tmp_mul ; | |
| 812 nut->frame_code[i].size_lsb = tmp_size ; | |
| 813 if(++tmp_size >= tmp_mul){ | |
| 814 tmp_size=0; | |
| 815 tmp_stream++; | |
| 816 } | |
| 817 } | |
| 818 } | |
| 819 if(nut->frame_code['N'].flags != 1){ | |
| 820 av_log(s, AV_LOG_ERROR, "illegal frame_code table\n"); | |
| 821 return -1; | |
| 822 } | |
| 823 | |
| 220 | 824 get_be32(bc); /* checkusm */ |
| 219 | 825 |
| 826 s->bit_rate = 0; | |
| 328 | 827 |
| 403 | 828 nut->stream = av_malloc(sizeof(StreamContext)*nb_streams); |
| 219 | 829 |
| 830 /* stream header */ | |
| 831 for (cur_stream = 0; cur_stream < nb_streams; cur_stream++) | |
| 832 { | |
| 271 | 833 int class, nom, denom; |
| 219 | 834 AVStream *st; |
| 835 | |
| 220 | 836 tmp = get_be64(bc); |
| 837 if (tmp != STREAM_STARTCODE) | |
|
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
328
diff
changeset
|
838 av_log(s, AV_LOG_ERROR, "damaged? startcode!=1 (%Ld)\n", tmp); |
| 403 | 839 get_packetheader(nut, bc, 8); |
| 219 | 840 st = av_new_stream(s, get_v(bc)); |
| 841 if (!st) | |
| 842 return AVERROR_NOMEM; | |
| 843 class = get_v(bc); | |
| 415 | 844 tmp = get_v(bc); |
| 219 | 845 switch(class) |
| 846 { | |
| 847 case 0: | |
| 848 st->codec.codec_type = CODEC_TYPE_VIDEO; | |
| 849 st->codec.codec_id = codec_get_bmp_id(tmp); | |
| 850 if (st->codec.codec_id == CODEC_ID_NONE) | |
|
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
328
diff
changeset
|
851 av_log(s, AV_LOG_ERROR, "Unknown codec?!\n"); |
| 219 | 852 break; |
| 853 case 32: | |
| 854 st->codec.codec_type = CODEC_TYPE_AUDIO; | |
| 855 st->codec.codec_id = codec_get_wav_id(tmp); | |
| 856 if (st->codec.codec_id == CODEC_ID_NONE) | |
|
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
328
diff
changeset
|
857 av_log(s, AV_LOG_ERROR, "Unknown codec?!\n"); |
| 219 | 858 break; |
| 859 default: | |
|
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
328
diff
changeset
|
860 av_log(s, AV_LOG_ERROR, "Unknown stream class (%d)\n", class); |
| 219 | 861 return -1; |
| 862 } | |
| 863 s->bit_rate += get_v(bc); | |
| 415 | 864 get_v(bc); /* language code */ |
| 271 | 865 nom = get_v(bc); |
| 866 denom = get_v(bc); | |
| 403 | 867 nut->stream[cur_stream].msb_timestamp_shift = get_v(bc); |
| 868 for(i=0; i<3; i++) | |
| 869 nut->stream[cur_stream].initial_pts_predictor[i]= get_v(bc); | |
| 870 for(i=0; i<2; i++) | |
| 871 nut->stream[cur_stream].initial_size_predictor[i]= get_v(bc); | |
| 219 | 872 get_byte(bc); /* flags */ |
| 272 | 873 |
| 874 /* codec specific data headers */ | |
| 403 | 875 while(get_v(bc) != 0){ |
| 876 st->codec.extradata_size= get_v(bc); | |
| 877 st->codec.extradata= av_mallocz(st->codec.extradata_size); | |
| 878 get_buffer(bc, st->codec.extradata, st->codec.extradata_size); | |
| 879 // url_fskip(bc, get_v(bc)); | |
| 880 } | |
| 219 | 881 |
| 882 if (class == 0) /* VIDEO */ | |
| 883 { | |
| 884 st->codec.width = get_v(bc); | |
| 885 st->codec.height = get_v(bc); | |
| 403 | 886 st->codec.sample_aspect_ratio.num= get_v(bc); |
| 887 st->codec.sample_aspect_ratio.den= get_v(bc); | |
| 219 | 888 get_v(bc); /* csp type */ |
| 271 | 889 |
| 890 st->codec.frame_rate = nom; | |
| 891 st->codec.frame_rate_base = denom; | |
| 219 | 892 } |
| 893 if (class == 32) /* AUDIO */ | |
| 894 { | |
| 271 | 895 st->codec.sample_rate = (get_v(bc) * nom) / denom; |
| 219 | 896 st->codec.channels = get_v(bc); |
| 897 } | |
| 403 | 898 get_be32(bc); /* checksum */ |
| 899 nut->stream[cur_stream].rate_num= nom; | |
| 900 nut->stream[cur_stream].rate_den= denom; | |
| 415 | 901 } |
| 902 | |
| 903 tmp = get_be64(bc); | |
| 904 if (tmp == INFO_STARTCODE){ | |
| 905 get_packetheader(nut, bc, 8); | |
| 906 | |
| 907 for(;;){ | |
| 908 int id= get_v(bc); | |
| 909 char *name, *type, custom_name[256], custom_type[256]; | |
| 910 | |
| 911 if(!id) | |
| 912 break; | |
| 913 else if(id >= sizeof(info_table)/sizeof(info_table[0])){ | |
| 914 av_log(s, AV_LOG_ERROR, "info id is too large %d %d\n", id, sizeof(info_table)/sizeof(info_table[0])); | |
| 915 return -1; | |
| 916 } | |
| 917 | |
| 918 type= info_table[id][1]; | |
| 919 name= info_table[id][0]; | |
| 920 //av_log(s, AV_LOG_DEBUG, "%d %s %s\n", id, type, name); | |
| 921 | |
| 922 if(!type){ | |
| 923 get_str(bc, custom_type, sizeof(custom_type)); | |
| 924 type= custom_type; | |
| 925 } | |
| 926 if(!name){ | |
| 927 get_str(bc, custom_name, sizeof(custom_name)); | |
| 928 name= custom_name; | |
| 929 } | |
| 930 | |
| 931 if(!strcmp(type, "v")){ | |
| 932 int value= get_v(bc); | |
| 933 }else{ | |
| 934 if(!strcmp(name, "Author")) | |
| 935 get_str(bc, s->author, sizeof(s->author)); | |
| 936 else if(!strcmp(name, "Title")) | |
| 937 get_str(bc, s->title, sizeof(s->title)); | |
| 938 else if(!strcmp(name, "Copyright")) | |
| 939 get_str(bc, s->copyright, sizeof(s->copyright)); | |
| 940 else if(!strcmp(name, "Description")) | |
| 941 get_str(bc, s->comment, sizeof(s->comment)); | |
| 942 else | |
| 943 get_str(bc, NULL, 0); | |
| 944 } | |
| 945 } | |
| 946 get_be32(bc); /* checksum */ | |
| 947 }else | |
| 948 url_fseek(bc, -8, SEEK_CUR); | |
| 219 | 949 |
| 950 return 0; | |
| 951 } | |
| 952 | |
| 953 static int nut_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 954 { | |
| 955 NUTContext *nut = s->priv_data; | |
| 403 | 956 StreamContext *stream; |
| 219 | 957 ByteIOContext *bc = &s->pb; |
| 403 | 958 int size, frame_code, flags, size_mul, size_lsb, stream_id; |
| 219 | 959 int key_frame = 0; |
| 403 | 960 int frame_type= 0; |
| 328 | 961 int64_t pts = 0; |
| 403 | 962 const int64_t frame_start= url_ftell(bc); |
| 219 | 963 |
| 964 if (url_feof(bc)) | |
| 965 return -1; | |
| 966 | |
| 403 | 967 frame_code = get_byte(bc); |
| 968 if(frame_code == 'N'){ | |
| 969 uint64_t tmp= frame_code; | |
| 220 | 970 tmp<<=8 ; tmp |= get_byte(bc); |
| 971 tmp<<=16; tmp |= get_be16(bc); | |
| 972 tmp<<=32; tmp |= get_be32(bc); | |
| 973 if (tmp == KEYFRAME_STARTCODE) | |
| 219 | 974 { |
| 403 | 975 frame_code = get_byte(bc); |
| 976 frame_type = 2; | |
| 219 | 977 } |
| 978 else | |
|
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
328
diff
changeset
|
979 av_log(s, AV_LOG_ERROR, "error in zero bit / startcode %LX\n", tmp); |
| 219 | 980 } |
| 403 | 981 flags= nut->frame_code[frame_code].flags; |
| 982 size_mul= nut->frame_code[frame_code].size_mul; | |
| 983 size_lsb= nut->frame_code[frame_code].size_lsb; | |
| 984 stream_id= nut->frame_code[frame_code].stream_id_plus1 - 1; | |
| 985 | |
| 986 if(flags & FLAG_FRAME_TYPE){ | |
| 987 reset(s); | |
| 988 if(frame_type==2){ | |
| 989 get_packetheader(nut, bc, 8+1); | |
| 990 }else{ | |
| 991 get_packetheader(nut, bc, 1); | |
| 992 frame_type= 1; | |
| 993 } | |
| 994 } | |
| 995 | |
| 996 if(stream_id==-1) | |
| 997 stream_id= get_v(bc); | |
| 998 if(stream_id >= s->nb_streams){ | |
| 999 av_log(s, AV_LOG_ERROR, "illegal stream_id\n"); | |
| 1000 return -1; | |
| 1001 } | |
| 1002 stream= &nut->stream[stream_id]; | |
| 219 | 1003 |
| 403 | 1004 if(flags & FLAG_PRED_KEY_FRAME){ |
| 1005 if(flags & FLAG_KEY_FRAME) | |
| 1006 key_frame= !stream->last_key_frame; | |
| 1007 else | |
| 1008 key_frame= stream->last_key_frame; | |
| 1009 }else{ | |
| 1010 key_frame= !!(flags & FLAG_KEY_FRAME); | |
| 1011 } | |
| 1012 | |
| 1013 if(flags & FLAG_PTS){ | |
| 1014 if(flags & FLAG_FULL_PTS){ | |
| 1015 pts= get_v(bc); | |
| 1016 }else{ | |
| 1017 int64_t mask = (1<<stream->msb_timestamp_shift)-1; | |
| 1018 int64_t delta= stream->last_pts - mask/2; | |
| 1019 pts= ((get_v(bc) - delta)&mask) + delta; | |
| 1020 } | |
| 1021 }else{ | |
| 1022 pts= stream->last_pts + stream->lru_pts_delta[(flags&12)>>2]; | |
| 1023 } | |
| 1024 | |
| 1025 if(size_mul <= size_lsb){ | |
| 1026 size= stream->lru_size[size_lsb - size_mul]; | |
| 1027 }else{ | |
| 1028 if(flags & FLAG_DATA_SIZE) | |
| 1029 size= size_mul*get_v(bc) + size_lsb; | |
| 1030 else | |
| 1031 size= size_lsb; | |
| 1032 } | |
| 1033 | |
| 1034 //av_log(s, AV_LOG_DEBUG, "fs:%lld fc:%d ft:%d kf:%d pts:%lld\n", frame_start, frame_code, frame_type, key_frame, pts); | |
| 219 | 1035 |
| 1036 av_new_packet(pkt, size); | |
| 1037 get_buffer(bc, pkt->data, size); | |
| 403 | 1038 pkt->stream_index = stream_id; |
| 219 | 1039 if (key_frame) |
| 1040 pkt->flags |= PKT_FLAG_KEY; | |
| 403 | 1041 pkt->pts = pts * AV_TIME_BASE * stream->rate_den / stream->rate_num; |
| 1042 | |
| 1043 update(nut, stream_id, frame_start, frame_type, frame_code, key_frame, size, pts); | |
| 1044 | |
| 1045 return 0; | |
| 1046 } | |
| 1047 | |
| 1048 static int nut_read_close(AVFormatContext *s) | |
| 1049 { | |
| 1050 NUTContext *nut = s->priv_data; | |
| 1051 int i; | |
| 1052 | |
| 1053 for(i=0;i<s->nb_streams;i++) { | |
| 1054 av_freep(&s->streams[i]->codec.extradata); | |
| 1055 } | |
| 1056 av_freep(&nut->stream); | |
| 219 | 1057 |
| 1058 return 0; | |
| 1059 } | |
| 1060 | |
| 1061 static AVInputFormat nut_iformat = { | |
| 1062 "nut", | |
| 1063 "nut format", | |
| 1064 sizeof(NUTContext), | |
| 1065 nut_probe, | |
| 1066 nut_read_header, | |
| 1067 nut_read_packet, | |
| 403 | 1068 nut_read_close, |
| 219 | 1069 // nut_read_seek, |
| 1070 .extensions = "nut", | |
| 1071 }; | |
| 1072 | |
|
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
272
diff
changeset
|
1073 #ifdef CONFIG_ENCODERS |
| 219 | 1074 static AVOutputFormat nut_oformat = { |
| 1075 "nut", | |
| 1076 "nut format", | |
| 1077 "video/x-nut", | |
| 1078 "nut", | |
| 1079 sizeof(NUTContext), | |
| 414 | 1080 #ifdef CONFIG_VORBIS |
| 219 | 1081 CODEC_ID_VORBIS, |
| 1082 #elif defined(CONFIG_MP3LAME) | |
| 232 | 1083 CODEC_ID_MP3, |
| 219 | 1084 #else |
| 223 | 1085 CODEC_ID_MP2, /* AC3 needs liba52 decoder */ |
| 219 | 1086 #endif |
| 1087 CODEC_ID_MPEG4, | |
| 1088 nut_write_header, | |
| 1089 nut_write_packet, | |
| 1090 nut_write_trailer, | |
| 1091 }; | |
|
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
272
diff
changeset
|
1092 #endif //CONFIG_ENCODERS |
| 219 | 1093 |
| 1094 int nut_init(void) | |
| 1095 { | |
| 1096 av_register_input_format(&nut_iformat); | |
|
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
272
diff
changeset
|
1097 #ifdef CONFIG_ENCODERS |
| 219 | 1098 av_register_output_format(&nut_oformat); |
|
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
272
diff
changeset
|
1099 #endif //CONFIG_ENCODERS |
| 219 | 1100 return 0; |
| 1101 } |
