2 * "NUT" Container Format demuxer
3 * Copyright (c) 2004-2006 Michael Niedermayer
4 * Copyright (c) 2003 Alex Beregszaszi
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 static uint64_t get_v(ByteIOContext *bc){
36 val= (val<<7) + (tmp&127);
41 static int get_str(ByteIOContext *bc, char *string, unsigned int maxlen){
42 unsigned int len= get_v(bc);
45 get_buffer(bc, string, FFMIN(len, maxlen));
52 string[FFMIN(len, maxlen-1)]= 0;
60 static int64_t get_s(ByteIOContext *bc){
61 int64_t v = get_v(bc) + 1;
63 if (v&1) return -(v>>1);
67 static uint64_t get_fourcc(ByteIOContext *bc){
68 unsigned int len= get_v(bc);
70 if (len==2) return get_le16(bc);
71 else if(len==4) return get_le32(bc);
76 static inline uint64_t get_v_trace(ByteIOContext *bc, char *file, char *func, int line){
77 uint64_t v= get_v(bc);
79 printf("get_v %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
83 static inline int64_t get_s_trace(ByteIOContext *bc, char *file, char *func, int line){
86 printf("get_s %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
90 static inline uint64_t get_vb_trace(ByteIOContext *bc, char *file, char *func, int line){
91 uint64_t v= get_vb(bc);
93 printf("get_vb %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
96 #define get_v(bc) get_v_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
97 #define get_s(bc) get_s_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
98 #define get_vb(bc) get_vb_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
101 static int get_packetheader(NUTContext *nut, ByteIOContext *bc, int calculate_checksum)
104 // start= url_ftell(bc) - 8;
108 get_be32(bc); //FIXME check this
110 init_checksum(bc, calculate_checksum ? av_crc04C11DB7_update : NULL, 0);
112 // nut->packet_start[2] = start;
113 // nut->written_packet_size= size;
118 static uint64_t find_any_startcode(ByteIOContext *bc, int64_t pos){
122 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
124 while(!url_feof(bc)){
125 state= (state<<8) | get_byte(bc);
126 if((state>>56) != 'N')
130 case STREAM_STARTCODE:
131 case SYNCPOINT_STARTCODE:
133 case INDEX_STARTCODE:
142 * find the given startcode.
143 * @param code the startcode
144 * @param pos the start position of the search, or -1 if the current position
145 * @returns the position of the startcode or -1 if not found
147 static int64_t find_startcode(ByteIOContext *bc, uint64_t code, int64_t pos){
149 uint64_t startcode= find_any_startcode(bc, pos);
150 if(startcode == code)
151 return url_ftell(bc) - 8;
152 else if(startcode == 0)
158 static int64_t lsb2full(StreamContext *stream, int64_t lsb){
159 int64_t mask = (1<<stream->msb_pts_shift)-1;
160 int64_t delta= stream->last_pts - mask/2;
161 return ((lsb - delta)&mask) + delta;
164 static int nut_probe(AVProbeData *p){
168 for (i = 0; i < p->buf_size; i++) {
169 code = (code << 8) | p->buf[i];
170 if (code == MAIN_STARTCODE)
171 return AVPROBE_SCORE_MAX;
176 #define GET_V(dst, check) \
179 av_log(s, AV_LOG_ERROR, "Error " #dst " is (%"PRId64")\n", tmp);\
184 static int skip_reserved(ByteIOContext *bc, int64_t pos){
185 pos -= url_ftell(bc);
187 url_fseek(bc, pos, SEEK_CUR);
196 static int decode_main_header(NUTContext *nut){
197 AVFormatContext *s= nut->avf;
198 ByteIOContext *bc = &s->pb;
200 unsigned int stream_count;
201 int i, j, tmp_stream, tmp_mul, tmp_pts, tmp_size, count, tmp_res;
203 end= get_packetheader(nut, bc, 1);
204 end += url_ftell(bc);
206 GET_V(tmp , tmp >=2 && tmp <= 3)
207 GET_V(stream_count , tmp > 0 && tmp <=MAX_STREAMS)
209 nut->max_distance = get_v(bc);
210 if(nut->max_distance > 65536){
211 av_log(s, AV_LOG_DEBUG, "max_distance %d\n", nut->max_distance);
212 nut->max_distance= 65536;
215 GET_V(nut->time_base_count, tmp>0 && tmp<INT_MAX / sizeof(AVRational))
216 nut->time_base= av_malloc(nut->time_base_count * sizeof(AVRational));
218 for(i=0; i<nut->time_base_count; i++){
219 GET_V(nut->time_base[i].num, tmp>0 && tmp<(1ULL<<31))
220 GET_V(nut->time_base[i].den, tmp>0 && tmp<(1ULL<<31))
221 if(ff_gcd(nut->time_base[i].num, nut->time_base[i].den) != 1){
222 av_log(s, AV_LOG_ERROR, "time base invalid\n");
230 int tmp_flags = get_v(bc);
231 int tmp_fields= get_v(bc);
232 if(tmp_fields>0) tmp_pts = get_s(bc);
233 if(tmp_fields>1) tmp_mul = get_v(bc);
234 if(tmp_fields>2) tmp_stream= get_v(bc);
235 if(tmp_fields>3) tmp_size = get_v(bc);
237 if(tmp_fields>4) tmp_res = get_v(bc);
239 if(tmp_fields>5) count = get_v(bc);
240 else count = tmp_mul - tmp_size;
242 while(tmp_fields-- > 6)
245 if(count == 0 || i+count > 256){
246 av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
249 if(tmp_stream >= stream_count){
250 av_log(s, AV_LOG_ERROR, "illegal stream number\n");
254 for(j=0; j<count; j++,i++){
256 nut->frame_code[i].flags= FLAG_INVALID;
260 nut->frame_code[i].flags = tmp_flags ;
261 nut->frame_code[i].pts_delta = tmp_pts ;
262 nut->frame_code[i].stream_id = tmp_stream;
263 nut->frame_code[i].size_mul = tmp_mul ;
264 nut->frame_code[i].size_lsb = tmp_size+j;
265 nut->frame_code[i].reserved_count = tmp_res ;
268 assert(nut->frame_code['N'].flags == FLAG_INVALID);
270 if(skip_reserved(bc, end) || get_checksum(bc)){
271 av_log(s, AV_LOG_ERROR, "Main header checksum mismatch\n");
275 nut->stream = av_mallocz(sizeof(StreamContext)*stream_count);
276 for(i=0; i<stream_count; i++){
283 static int decode_stream_header(NUTContext *nut){
284 AVFormatContext *s= nut->avf;
285 ByteIOContext *bc = &s->pb;
287 int class, stream_id;
291 end= get_packetheader(nut, bc, 1);
292 end += url_ftell(bc);
294 GET_V(stream_id, tmp < s->nb_streams && !nut->stream[tmp].time_base.num);
295 stc= &nut->stream[stream_id];
297 st = s->streams[stream_id];
299 return AVERROR_NOMEM;
302 tmp = get_fourcc(bc);
303 st->codec->codec_tag= tmp;
307 st->codec->codec_type = CODEC_TYPE_VIDEO;
308 st->codec->codec_id = codec_get_bmp_id(tmp);
309 if (st->codec->codec_id == CODEC_ID_NONE)
310 av_log(s, AV_LOG_ERROR, "Unknown codec?!\n");
313 st->codec->codec_type = CODEC_TYPE_AUDIO;
314 st->codec->codec_id = codec_get_wav_id(tmp);
315 if (st->codec->codec_id == CODEC_ID_NONE)
316 av_log(s, AV_LOG_ERROR, "Unknown codec?!\n");
319 // st->codec->codec_type = CODEC_TYPE_TEXT;
322 st->codec->codec_type = CODEC_TYPE_DATA;
325 av_log(s, AV_LOG_ERROR, "Unknown stream class (%d)\n", class);
328 GET_V(stc->time_base_id , tmp < nut->time_base_count);
329 GET_V(stc->msb_pts_shift , tmp < 16);
330 stc->max_pts_distance= get_v(bc);
331 GET_V(stc->decode_delay , tmp < 1000); //sanity limit, raise this if moors law is true
332 st->codec->has_b_frames= stc->decode_delay;
333 get_v(bc); //stream flags
335 GET_V(st->codec->extradata_size, tmp < (1<<30));
336 if(st->codec->extradata_size){
337 st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
338 get_buffer(bc, st->codec->extradata, st->codec->extradata_size);
341 if (st->codec->codec_type == CODEC_TYPE_VIDEO){
342 GET_V(st->codec->width , tmp > 0)
343 GET_V(st->codec->height, tmp > 0)
344 st->codec->sample_aspect_ratio.num= get_v(bc);
345 st->codec->sample_aspect_ratio.den= get_v(bc);
346 if((!st->codec->sample_aspect_ratio.num) != (!st->codec->sample_aspect_ratio.den)){
347 av_log(s, AV_LOG_ERROR, "invalid aspect ratio\n");
350 get_v(bc); /* csp type */
351 }else if (st->codec->codec_type == CODEC_TYPE_AUDIO){
352 GET_V(st->codec->sample_rate , tmp > 0)
353 tmp= get_v(bc); // samplerate_den
354 if(tmp > st->codec->sample_rate){
355 av_log(s, AV_LOG_ERROR, "bleh, libnut muxed this ;)\n");
356 st->codec->sample_rate= tmp;
358 GET_V(st->codec->channels, tmp > 0)
360 if(skip_reserved(bc, end) || get_checksum(bc)){
361 av_log(s, AV_LOG_ERROR, "Stream header %d checksum mismatch\n", stream_id);
364 stc->time_base= nut->time_base[stc->time_base_id];
365 av_set_pts_info(s->streams[stream_id], 63, stc->time_base.num, stc->time_base.den);
369 static int decode_info_header(NUTContext *nut){
370 AVFormatContext *s= nut->avf;
371 ByteIOContext *bc = &s->pb;
373 unsigned int stream_id_plus1, chapter_start, chapter_len, count;
376 char name[256], str_value[1024], type_str[256], *type= type_str;
378 end= get_packetheader(nut, bc, 1);
379 end += url_ftell(bc);
381 GET_V(stream_id_plus1, tmp <= s->nb_streams)
382 chapter_id = get_s(bc);
383 chapter_start= get_v(bc);
384 chapter_len = get_v(bc);
386 for(i=0; i<count; i++){
387 get_str(bc, name, sizeof(name));
391 get_str(bc, str_value, sizeof(str_value));
392 }else if(value == -2){
393 get_str(bc, type, sizeof(type));
394 get_str(bc, str_value, sizeof(str_value));
395 }else if(value == -3){
398 }else if(value == -4){
401 }else if(value < -4){
408 if(chapter_id==0 && !strcmp(type, "UTF-8")){
409 if (!strcmp(name, "Author"))
410 pstrcpy(s->author , sizeof(s->author) , str_value);
411 else if(!strcmp(name, "Title"))
412 pstrcpy(s->title , sizeof(s->title) , str_value);
413 else if(!strcmp(name, "Copyright"))
414 pstrcpy(s->copyright, sizeof(s->copyright), str_value);
415 else if(!strcmp(name, "Description"))
416 pstrcpy(s->comment , sizeof(s->comment) , str_value);
420 if(skip_reserved(bc, end) || get_checksum(bc)){
421 av_log(s, AV_LOG_ERROR, "Info header checksum mismatch\n");
427 int sp_pos_cmp(syncpoint_t *a, syncpoint_t *b){
428 return (a->pos - b->pos>>32) - (b->pos - a->pos>>32);
431 int sp_pts_cmp(syncpoint_t *a, syncpoint_t *b){
432 return (a->ts - b->ts>>32) - (b->ts - a->ts>>32);
435 static void add_sp(NUTContext *nut, int64_t pos, int64_t back_ptr, int64_t ts){
436 syncpoint_t *sp2, *sp= av_mallocz(sizeof(syncpoint_t));
439 sp->back_ptr= back_ptr;
441 sp2= av_tree_insert(&nut->syncpoints, sp, sp_pos_cmp);
446 static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr){
447 AVFormatContext *s= nut->avf;
448 ByteIOContext *bc = &s->pb;
451 AVRational time_base;
453 nut->last_syncpoint_pos= url_ftell(bc)-8;
455 end= get_packetheader(nut, bc, 1);
456 end += url_ftell(bc);
459 *back_ptr= nut->last_syncpoint_pos - 16*get_v(bc);
463 time_base= nut->time_base[tmp % nut->time_base_count];
464 for(i=0; i<s->nb_streams; i++){
465 nut->stream[i].last_pts= av_rescale_rnd(
466 tmp / nut->time_base_count,
467 time_base.num * (int64_t)nut->stream[i].time_base.den,
468 time_base.den * (int64_t)nut->stream[i].time_base.num,
472 //FIXME put this in a reset func maybe
474 if(skip_reserved(bc, end) || get_checksum(bc)){
475 av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n");
479 *ts= tmp / s->nb_streams * av_q2d(nut->time_base[tmp % s->nb_streams])*AV_TIME_BASE;
480 add_sp(nut, nut->last_syncpoint_pos, *back_ptr, *ts);
485 static int find_and_decode_index(NUTContext *nut){
486 AVFormatContext *s= nut->avf;
487 ByteIOContext *bc = &s->pb;
489 int i, j, syncpoint_count;
490 int64_t filesize= url_fsize(bc);
492 int8_t *has_keyframe;
494 url_fseek(bc, filesize-12, SEEK_SET);
495 url_fseek(bc, filesize-get_be64(bc), SEEK_SET);
496 if(get_be64(bc) != INDEX_STARTCODE){
497 av_log(s, AV_LOG_ERROR, "no index at the end\n");
501 end= get_packetheader(nut, bc, 1);
502 end += url_ftell(bc);
505 GET_V(syncpoint_count, tmp < INT_MAX/8 && tmp > 0)
506 syncpoints= av_malloc(sizeof(int64_t)*syncpoint_count);
507 has_keyframe= av_malloc(sizeof(int8_t)*(syncpoint_count+1));
508 for(i=0; i<syncpoint_count; i++){
509 GET_V(syncpoints[i], tmp>0)
511 syncpoints[i] += syncpoints[i-1];
514 for(i=0; i<s->nb_streams; i++){
515 int64_t last_pts= -1;
516 for(j=0; j<syncpoint_count;){
517 uint64_t x= get_v(bc);
524 if(n+x >= syncpoint_count + 1){
525 av_log(s, AV_LOG_ERROR, "index overflow A\n");
529 has_keyframe[n++]= flag;
530 has_keyframe[n++]= !flag;
533 if(n>=syncpoint_count + 1){
534 av_log(s, AV_LOG_ERROR, "index overflow B\n");
537 has_keyframe[n++]= x&1;
542 av_log(s, AV_LOG_ERROR, "keyframe before first syncpoint in index\n");
545 assert(n<=syncpoint_count+1);
548 uint64_t B, A= get_v(bc);
552 //eor_pts[j][i] = last_pts + A + B
568 if(skip_reserved(bc, end) || get_checksum(bc)){
569 av_log(s, AV_LOG_ERROR, "Index checksum mismatch\n");
575 static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
577 NUTContext *nut = s->priv_data;
578 ByteIOContext *bc = &s->pb;
580 int inited_stream_count;
587 pos= find_startcode(bc, MAIN_STARTCODE, pos)+1;
589 av_log(s, AV_LOG_ERROR, "no main startcode found\n");
592 }while(decode_main_header(nut) < 0);
596 for(inited_stream_count=0; inited_stream_count < s->nb_streams;){
597 pos= find_startcode(bc, STREAM_STARTCODE, pos)+1;
599 av_log(s, AV_LOG_ERROR, "not all stream headers found\n");
602 if(decode_stream_header(nut) >= 0)
603 inited_stream_count++;
609 uint64_t startcode= find_any_startcode(bc, pos);
613 av_log(s, AV_LOG_ERROR, "EOF before video frames\n");
615 }else if(startcode == SYNCPOINT_STARTCODE){
616 nut->next_startcode= startcode;
618 }else if(startcode != INFO_STARTCODE){
622 decode_info_header(nut);
625 s->data_offset= pos-8;
627 if(!url_is_streamed(bc)){
628 int64_t orig_pos= url_ftell(bc);
629 find_and_decode_index(nut);
630 url_fseek(bc, orig_pos, SEEK_SET);
632 assert(nut->next_startcode == SYNCPOINT_STARTCODE);
637 static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id, int frame_code){
638 AVFormatContext *s= nut->avf;
639 ByteIOContext *bc = &s->pb;
641 int size, flags, size_mul, pts_delta, i, reserved_count;
644 if(url_ftell(bc) > nut->last_syncpoint_pos + nut->max_distance){
645 av_log(s, AV_LOG_ERROR, "last frame must have been damaged %Ld > %Ld + %d\n", url_ftell(bc), nut->last_syncpoint_pos, nut->max_distance);
649 flags = nut->frame_code[frame_code].flags;
650 size_mul = nut->frame_code[frame_code].size_mul;
651 size = nut->frame_code[frame_code].size_lsb;
652 *stream_id = nut->frame_code[frame_code].stream_id;
653 pts_delta = nut->frame_code[frame_code].pts_delta;
654 reserved_count = nut->frame_code[frame_code].reserved_count;
656 if(flags & FLAG_INVALID)
658 if(flags & FLAG_CODED)
660 if(flags & FLAG_STREAM_ID){
661 GET_V(*stream_id, tmp < s->nb_streams)
663 stc= &nut->stream[*stream_id];
664 if(flags&FLAG_CODED_PTS){
665 int coded_pts= get_v(bc);
666 //FIXME check last_pts validity?
667 if(coded_pts < (1<<stc->msb_pts_shift)){
668 *pts=lsb2full(stc, coded_pts);
670 *pts=coded_pts - (1<<stc->msb_pts_shift);
672 *pts= stc->last_pts + pts_delta;
673 if(flags&FLAG_SIZE_MSB){
674 size += size_mul*get_v(bc);
676 if(flags&FLAG_RESERVED)
677 reserved_count= get_v(bc);
678 for(i=0; i<reserved_count; i++)
680 if(flags&FLAG_CHECKSUM){
681 get_be32(bc); //FIXME check this
682 }else if(size > 2*nut->max_distance || FFABS(stc->last_pts - *pts) > stc->max_pts_distance){
683 av_log(s, AV_LOG_ERROR, "frame size > 2max_distance and no checksum\n");
688 stc->last_flags= flags;
693 static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code){
694 AVFormatContext *s= nut->avf;
695 ByteIOContext *bc = &s->pb;
696 int size, stream_id, discard;
697 int64_t pts, last_IP_pts;
700 size= decode_frame_header(nut, &pts, &stream_id, frame_code);
704 stc= &nut->stream[stream_id];
706 if (stc->last_flags & FLAG_KEY)
707 stc->skip_until_key_frame=0;
709 discard= s->streams[ stream_id ]->discard;
710 last_IP_pts= s->streams[ stream_id ]->last_IP_pts;
711 if( (discard >= AVDISCARD_NONKEY && !(stc->last_flags & FLAG_KEY))
712 ||(discard >= AVDISCARD_BIDIR && last_IP_pts != AV_NOPTS_VALUE && last_IP_pts > pts)
713 || discard >= AVDISCARD_ALL
714 || stc->skip_until_key_frame){
719 av_get_packet(bc, pkt, size);
720 pkt->stream_index = stream_id;
721 if (stc->last_flags & FLAG_KEY)
722 pkt->flags |= PKT_FLAG_KEY;
728 static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
730 NUTContext *nut = s->priv_data;
731 ByteIOContext *bc = &s->pb;
732 int i, frame_code=0, ret, skip;
733 int64_t ts, back_ptr;
736 int64_t pos= url_ftell(bc);
737 uint64_t tmp= nut->next_startcode;
738 nut->next_startcode=0;
746 frame_code = get_byte(bc);
747 if(frame_code == 'N'){
750 tmp = (tmp<<8) + get_byte(bc);
755 case STREAM_STARTCODE:
756 case INDEX_STARTCODE:
757 skip= get_packetheader(nut, bc, 0);
758 url_fseek(bc, skip, SEEK_CUR);
761 if(decode_info_header(nut)<0)
764 case SYNCPOINT_STARTCODE:
765 if(decode_syncpoint(nut, &ts, &back_ptr)<0)
767 frame_code = get_byte(bc);
769 ret= decode_frame(nut, pkt, frame_code);
772 else if(ret==1) //ok but discard packet
776 av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", pos);
777 tmp= find_any_startcode(bc, nut->last_syncpoint_pos+1);
780 av_log(s, AV_LOG_DEBUG, "sync\n");
781 nut->next_startcode= tmp;
786 static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index, int64_t *pos_arg, int64_t pos_limit){
787 NUTContext *nut = s->priv_data;
788 ByteIOContext *bc = &s->pb;
789 int64_t pos, pts, back_ptr;
790 av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n", stream_index, *pos_arg, pos_limit);
795 pos= find_startcode(bc, SYNCPOINT_STARTCODE, pos)+1;
797 assert(nut->next_startcode == 0);
798 av_log(s, AV_LOG_ERROR, "read_timestamp failed\n");
799 return AV_NOPTS_VALUE;
801 }while(decode_syncpoint(nut, &pts, &back_ptr) < 0);
803 assert(nut->last_syncpoint_pos == *pos_arg);
805 av_log(s, AV_LOG_DEBUG, "return %Ld %Ld\n", pts,back_ptr );
806 if (stream_index == -1) return pts;
807 else if(stream_index == -2) return back_ptr;
812 static int read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags){
813 NUTContext *nut = s->priv_data;
814 AVStream *st= s->streams[stream_index];
815 syncpoint_t dummy={.ts= pts*av_q2d(st->time_base)*AV_TIME_BASE};
816 syncpoint_t nopts_sp= {.ts= AV_NOPTS_VALUE, .back_ptr= AV_NOPTS_VALUE};
817 syncpoint_t *sp, *next_node[2]= {&nopts_sp, &nopts_sp};
818 int64_t pos, pos2, ts;
821 if(st->index_entries){
822 int index= av_index_search_timestamp(st, pts, flags);
826 pos2= st->index_entries[index].pos;
827 ts = st->index_entries[index].timestamp;
829 av_tree_find(nut->syncpoints, &dummy, sp_pts_cmp, next_node);
830 av_log(s, AV_LOG_DEBUG, "%Ld-%Ld %Ld-%Ld\n", next_node[0]->pos, next_node[1]->pos,
831 next_node[0]->ts , next_node[1]->ts);
832 pos= av_gen_search(s, -1, dummy.ts, next_node[0]->pos, next_node[1]->pos, next_node[1]->pos,
833 next_node[0]->ts , next_node[1]->ts, AVSEEK_FLAG_BACKWARD, &ts, nut_read_timestamp);
835 if(!(flags & AVSEEK_FLAG_BACKWARD)){
837 next_node[1]= &nopts_sp;
838 av_tree_find(nut->syncpoints, &dummy, sp_pos_cmp, next_node);
839 pos2= av_gen_search(s, -2, dummy.pos, next_node[0]->pos , next_node[1]->pos, next_node[1]->pos,
840 next_node[0]->back_ptr, next_node[1]->back_ptr, flags, &ts, nut_read_timestamp);
843 //FIXME dir but i think it doesnt matter
846 sp= av_tree_find(nut->syncpoints, &dummy, sp_pos_cmp, NULL);
849 pos2= sp->back_ptr - 15;
851 av_log(NULL, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos2);
852 pos= find_startcode(&s->pb, SYNCPOINT_STARTCODE, pos2);
853 url_fseek(&s->pb, pos, SEEK_SET);
854 av_log(NULL, AV_LOG_DEBUG, "SP: %"PRId64"\n", pos);
855 if(pos2 > pos || pos2 + 15 < pos){
856 av_log(NULL, AV_LOG_ERROR, "no syncpoint at backptr pos\n");
858 for(i=0; i<s->nb_streams; i++)
859 nut->stream[i].skip_until_key_frame=1;
864 static int nut_read_close(AVFormatContext *s)
866 NUTContext *nut = s->priv_data;
868 av_freep(&nut->time_base);
869 av_freep(&nut->stream);
874 #ifdef CONFIG_NUT_DEMUXER
875 AVInputFormat nut_demuxer = {