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