You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

742 lines
21KB

  1. /*
  2. * "NUT" Container Format demuxer
  3. * Copyright (c) 2004-2006 Michael Niedermayer
  4. * Copyright (c) 2003 Alex Beregszaszi
  5. *
  6. * This file is part of FFmpeg.
  7. *
  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.
  12. *
  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.
  17. *
  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
  21. *
  22. */
  23. #include "nut.h"
  24. #undef NDEBUG
  25. #include <assert.h>
  26. static uint64_t get_v(ByteIOContext *bc/*, maxstuffing*/){
  27. uint64_t val = 0;
  28. for(;;)
  29. {
  30. int tmp = get_byte(bc);
  31. // if(tmp=0x80){
  32. // if(!maxstuffing-- || val)
  33. // return -1;
  34. // }
  35. if (tmp&0x80)
  36. val= (val<<7) + tmp - 0x80;
  37. else{
  38. return (val<<7) + tmp;
  39. }
  40. }
  41. return -1;
  42. }
  43. static int get_str(ByteIOContext *bc, char *string, unsigned int maxlen){
  44. unsigned int len= get_v(bc);
  45. if(len && maxlen)
  46. get_buffer(bc, string, FFMIN(len, maxlen));
  47. while(len > maxlen){
  48. get_byte(bc);
  49. len--;
  50. }
  51. if(maxlen)
  52. string[FFMIN(len, maxlen-1)]= 0;
  53. if(maxlen == len)
  54. return -1;
  55. else
  56. return 0;
  57. }
  58. static int64_t get_s(ByteIOContext *bc){
  59. int64_t v = get_v(bc) + 1;
  60. if (v&1) return -(v>>1);
  61. else return (v>>1);
  62. }
  63. static uint64_t get_fourcc(ByteIOContext *bc){
  64. unsigned int len= get_v(bc);
  65. if (len==2) return get_le16(bc);
  66. else if(len==4) return get_le32(bc);
  67. else return -1;
  68. }
  69. #ifdef TRACE
  70. static inline uint64_t get_v_trace(ByteIOContext *bc, char *file, char *func, int line){
  71. uint64_t v= get_v(bc);
  72. printf("get_v %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
  73. return v;
  74. }
  75. static inline int64_t get_s_trace(ByteIOContext *bc, char *file, char *func, int line){
  76. int64_t v= get_s(bc);
  77. printf("get_s %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
  78. return v;
  79. }
  80. static inline uint64_t get_vb_trace(ByteIOContext *bc, char *file, char *func, int line){
  81. uint64_t v= get_vb(bc);
  82. printf("get_vb %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
  83. return v;
  84. }
  85. #define get_v(bc) get_v_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  86. #define get_s(bc) get_s_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  87. #define get_vb(bc) get_vb_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  88. #endif
  89. static int get_packetheader(NUTContext *nut, ByteIOContext *bc, int calculate_checksum)
  90. {
  91. int64_t start, size;
  92. // start= url_ftell(bc) - 8;
  93. size= get_v(bc);
  94. init_checksum(bc, calculate_checksum ? av_crc04C11DB7_update : NULL, 1);
  95. // nut->packet_start[2] = start;
  96. // nut->written_packet_size= size;
  97. return size;
  98. }
  99. static int check_checksum(ByteIOContext *bc){
  100. unsigned long checksum= get_checksum(bc);
  101. // return checksum != get_be32(bc);
  102. av_log(NULL, AV_LOG_ERROR, "%08X %08X\n", checksum, (int)get_be32(bc));
  103. return 0;
  104. }
  105. static uint64_t find_any_startcode(ByteIOContext *bc, int64_t pos){
  106. uint64_t state=0;
  107. if(pos >= 0)
  108. 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
  109. while(!url_feof(bc)){
  110. state= (state<<8) | get_byte(bc);
  111. if((state>>56) != 'N')
  112. continue;
  113. switch(state){
  114. case MAIN_STARTCODE:
  115. case STREAM_STARTCODE:
  116. case SYNCPOINT_STARTCODE:
  117. case INFO_STARTCODE:
  118. case INDEX_STARTCODE:
  119. return state;
  120. }
  121. }
  122. return 0;
  123. }
  124. /**
  125. * find the given startcode.
  126. * @param code the startcode
  127. * @param pos the start position of the search, or -1 if the current position
  128. * @returns the position of the startcode or -1 if not found
  129. */
  130. static int64_t find_startcode(ByteIOContext *bc, uint64_t code, int64_t pos){
  131. for(;;){
  132. uint64_t startcode= find_any_startcode(bc, pos);
  133. if(startcode == code)
  134. return url_ftell(bc) - 8;
  135. else if(startcode == 0)
  136. return -1;
  137. pos=-1;
  138. }
  139. }
  140. static int64_t lsb2full(StreamContext *stream, int64_t lsb){
  141. int64_t mask = (1<<stream->msb_pts_shift)-1;
  142. int64_t delta= stream->last_pts - mask/2;
  143. return ((lsb - delta)&mask) + delta;
  144. }
  145. static int nut_probe(AVProbeData *p){
  146. int i;
  147. uint64_t code= 0;
  148. for (i = 0; i < p->buf_size; i++) {
  149. code = (code << 8) | p->buf[i];
  150. if (code == MAIN_STARTCODE)
  151. return AVPROBE_SCORE_MAX;
  152. }
  153. return 0;
  154. }
  155. #define GET_V(dst, check) \
  156. tmp= get_v(bc);\
  157. if(!(check)){\
  158. av_log(s, AV_LOG_ERROR, "Error " #dst " is (%"PRId64")\n", tmp);\
  159. return -1;\
  160. }\
  161. dst= tmp;
  162. static int skip_reserved(ByteIOContext *bc, int64_t pos){
  163. pos -= url_ftell(bc);
  164. if(pos<0){
  165. url_fseek(bc, pos, SEEK_CUR);
  166. return -1;
  167. }else{
  168. while(pos--)
  169. get_byte(bc);
  170. return 0;
  171. }
  172. }
  173. static int decode_main_header(NUTContext *nut){
  174. AVFormatContext *s= nut->avf;
  175. ByteIOContext *bc = &s->pb;
  176. uint64_t tmp, end;
  177. unsigned int stream_count;
  178. int i, j, tmp_stream, tmp_mul, tmp_pts, tmp_size, count, tmp_res;
  179. end= get_packetheader(nut, bc, 1);
  180. end += url_ftell(bc) - 4;
  181. GET_V(tmp , tmp >=2 && tmp <= 3)
  182. GET_V(stream_count , tmp > 0 && tmp <=MAX_STREAMS)
  183. nut->max_distance = get_v(bc);
  184. if(nut->max_distance > 65536){
  185. av_log(s, AV_LOG_DEBUG, "max_distance %d\n", nut->max_distance);
  186. nut->max_distance= 65536;
  187. }
  188. GET_V(nut->time_base_count, tmp>0 && tmp<INT_MAX / sizeof(AVRational))
  189. nut->time_base= av_malloc(nut->time_base_count * sizeof(AVRational));
  190. for(i=0; i<nut->time_base_count; i++){
  191. GET_V(nut->time_base[i].num, tmp>0 && tmp<(1ULL<<31))
  192. GET_V(nut->time_base[i].den, tmp>0 && tmp<(1ULL<<31))
  193. if(ff_gcd(nut->time_base[i].num, nut->time_base[i].den) != 1){
  194. av_log(s, AV_LOG_ERROR, "time base invalid\n");
  195. return -1;
  196. }
  197. }
  198. tmp_pts=0;
  199. tmp_mul=1;
  200. tmp_stream=0;
  201. for(i=0; i<256;){
  202. int tmp_flags = get_v(bc);
  203. int tmp_fields= get_v(bc);
  204. if(tmp_fields>0) tmp_pts = get_s(bc);
  205. if(tmp_fields>1) tmp_mul = get_v(bc);
  206. if(tmp_fields>2) tmp_stream= get_v(bc);
  207. if(tmp_fields>3) tmp_size = get_v(bc);
  208. else tmp_size = 0;
  209. if(tmp_fields>4) tmp_res = get_v(bc);
  210. else tmp_res = 0;
  211. if(tmp_fields>5) count = get_v(bc);
  212. else count = tmp_mul - tmp_size;
  213. while(tmp_fields-- > 6)
  214. get_v(bc);
  215. if(count == 0 || i+count > 256){
  216. av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
  217. return -1;
  218. }
  219. if(tmp_stream >= stream_count){
  220. av_log(s, AV_LOG_ERROR, "illegal stream number\n");
  221. return -1;
  222. }
  223. for(j=0; j<count; j++,i++){
  224. if (i == 'N') {
  225. nut->frame_code[i].flags= FLAG_INVALID;
  226. j--;
  227. continue;
  228. }
  229. nut->frame_code[i].flags = tmp_flags ;
  230. nut->frame_code[i].pts_delta = tmp_pts ;
  231. nut->frame_code[i].stream_id = tmp_stream;
  232. nut->frame_code[i].size_mul = tmp_mul ;
  233. nut->frame_code[i].size_lsb = tmp_size+j;
  234. nut->frame_code[i].reserved_count = tmp_res ;
  235. }
  236. }
  237. assert(nut->frame_code['N'].flags == FLAG_INVALID);
  238. if(skip_reserved(bc, end) || check_checksum(bc)){
  239. av_log(s, AV_LOG_ERROR, "Main header checksum mismatch\n");
  240. return -1;
  241. }
  242. nut->stream = av_mallocz(sizeof(StreamContext)*stream_count);
  243. for(i=0; i<stream_count; i++){
  244. av_new_stream(s, i);
  245. }
  246. return 0;
  247. }
  248. static int decode_stream_header(NUTContext *nut){
  249. AVFormatContext *s= nut->avf;
  250. ByteIOContext *bc = &s->pb;
  251. StreamContext *stc;
  252. int class, nom, denom, stream_id;
  253. uint64_t tmp, end;
  254. AVStream *st;
  255. end= get_packetheader(nut, bc, 1);
  256. end += url_ftell(bc) - 4;
  257. GET_V(stream_id, tmp < s->nb_streams && !nut->stream[tmp].time_base.num);
  258. stc= &nut->stream[stream_id];
  259. st = s->streams[stream_id];
  260. if (!st)
  261. return AVERROR_NOMEM;
  262. class = get_v(bc);
  263. tmp = get_fourcc(bc);
  264. st->codec->codec_tag= tmp;
  265. switch(class)
  266. {
  267. case 0:
  268. st->codec->codec_type = CODEC_TYPE_VIDEO;
  269. st->codec->codec_id = codec_get_bmp_id(tmp);
  270. if (st->codec->codec_id == CODEC_ID_NONE)
  271. av_log(s, AV_LOG_ERROR, "Unknown codec?!\n");
  272. break;
  273. case 1:
  274. st->codec->codec_type = CODEC_TYPE_AUDIO;
  275. st->codec->codec_id = codec_get_wav_id(tmp);
  276. if (st->codec->codec_id == CODEC_ID_NONE)
  277. av_log(s, AV_LOG_ERROR, "Unknown codec?!\n");
  278. break;
  279. case 2:
  280. // st->codec->codec_type = CODEC_TYPE_TEXT;
  281. // break;
  282. case 3:
  283. st->codec->codec_type = CODEC_TYPE_DATA;
  284. break;
  285. default:
  286. av_log(s, AV_LOG_ERROR, "Unknown stream class (%d)\n", class);
  287. return -1;
  288. }
  289. GET_V(stc->time_base_id , tmp < nut->time_base_count);
  290. GET_V(stc->msb_pts_shift , tmp < 16);
  291. stc->max_pts_distance= get_v(bc);
  292. GET_V(stc->decode_delay , tmp < 1000); //sanity limit, raise this if moors law is true
  293. st->codec->has_b_frames= stc->decode_delay;
  294. get_v(bc); //stream flags
  295. GET_V(st->codec->extradata_size, tmp < (1<<30));
  296. if(st->codec->extradata_size){
  297. st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  298. get_buffer(bc, st->codec->extradata, st->codec->extradata_size);
  299. }
  300. if (st->codec->codec_type == CODEC_TYPE_VIDEO){
  301. GET_V(st->codec->width , tmp > 0)
  302. GET_V(st->codec->height, tmp > 0)
  303. st->codec->sample_aspect_ratio.num= get_v(bc);
  304. st->codec->sample_aspect_ratio.den= get_v(bc);
  305. if((!st->codec->sample_aspect_ratio.num) != (!st->codec->sample_aspect_ratio.den)){
  306. av_log(s, AV_LOG_ERROR, "invalid aspect ratio\n");
  307. return -1;
  308. }
  309. get_v(bc); /* csp type */
  310. }else if (st->codec->codec_type == CODEC_TYPE_AUDIO){
  311. GET_V(st->codec->sample_rate , tmp > 0)
  312. tmp= get_v(bc); // samplerate_den
  313. if(tmp > st->codec->sample_rate){
  314. av_log(s, AV_LOG_ERROR, "bleh, libnut muxed this ;)\n");
  315. st->codec->sample_rate= tmp;
  316. }
  317. GET_V(st->codec->channels, tmp > 0)
  318. }
  319. if(skip_reserved(bc, end) || check_checksum(bc)){
  320. av_log(s, AV_LOG_ERROR, "Stream header %d checksum mismatch\n", stream_id);
  321. return -1;
  322. }
  323. stc->time_base= nut->time_base[stc->time_base_id];
  324. av_set_pts_info(s->streams[stream_id], 63, stc->time_base.num, stc->time_base.den);
  325. return 0;
  326. }
  327. static int decode_info_header(NUTContext *nut){
  328. AVFormatContext *s= nut->avf;
  329. ByteIOContext *bc = &s->pb;
  330. uint64_t tmp;
  331. unsigned int stream_id_plus1, chapter_start, chapter_len, count;
  332. int chapter_id, i;
  333. int64_t value, end;
  334. char name[256], str_value[1024], type_str[256], *type= type_str;
  335. end= get_packetheader(nut, bc, 1);
  336. end += url_ftell(bc) - 4;
  337. GET_V(stream_id_plus1, tmp <= s->nb_streams)
  338. chapter_id = get_s(bc);
  339. chapter_start= get_v(bc);
  340. chapter_len = get_v(bc);
  341. count = get_v(bc);
  342. for(i=0; i<count; i++){
  343. get_str(bc, name, sizeof(name));
  344. value= get_s(bc);
  345. if(value == -1){
  346. type= "UTF-8";
  347. get_str(bc, str_value, sizeof(str_value));
  348. }else if(value == -2){
  349. get_str(bc, type, sizeof(type));
  350. get_str(bc, str_value, sizeof(str_value));
  351. }else if(value == -3){
  352. type= "s";
  353. value= get_s(bc);
  354. }else if(value == -4){
  355. type= "t";
  356. value= get_v(bc);
  357. }else if(value < -4){
  358. type= "r";
  359. get_s(bc);
  360. }else{
  361. type= "v";
  362. }
  363. if(chapter_id==0 && !strcmp(type, "UTF-8")){
  364. if (!strcmp(name, "Author"))
  365. pstrcpy(s->author , sizeof(s->author) , str_value);
  366. else if(!strcmp(name, "Title"))
  367. pstrcpy(s->title , sizeof(s->title) , str_value);
  368. else if(!strcmp(name, "Copyright"))
  369. pstrcpy(s->copyright, sizeof(s->copyright), str_value);
  370. else if(!strcmp(name, "Description"))
  371. pstrcpy(s->comment , sizeof(s->comment) , str_value);
  372. }
  373. }
  374. if(skip_reserved(bc, end) || check_checksum(bc)){
  375. av_log(s, AV_LOG_ERROR, "Info header checksum mismatch\n");
  376. return -1;
  377. }
  378. return 0;
  379. }
  380. static int decode_syncpoint(NUTContext *nut){
  381. AVFormatContext *s= nut->avf;
  382. ByteIOContext *bc = &s->pb;
  383. int64_t end;
  384. uint64_t tmp;
  385. int i;
  386. AVRational time_base;
  387. nut->last_syncpoint_pos= url_ftell(bc)-8;
  388. end= get_packetheader(nut, bc, 1);
  389. end += url_ftell(bc) - 4;
  390. tmp= get_v(bc);
  391. get_v(bc); //back_ptr_div16
  392. time_base= nut->time_base[tmp % nut->time_base_count];
  393. for(i=0; i<s->nb_streams; i++){
  394. nut->stream[i].last_pts= av_rescale_q(tmp / nut->time_base_count, time_base, nut->stream[i].time_base); //FIXME rounding and co
  395. //last_key_frame ?
  396. }
  397. //FIXME put this in a reset func maybe
  398. if(skip_reserved(bc, end) || check_checksum(bc)){
  399. av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n");
  400. return -1;
  401. }
  402. return 0;
  403. }
  404. static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
  405. {
  406. NUTContext *nut = s->priv_data;
  407. ByteIOContext *bc = &s->pb;
  408. int64_t pos;
  409. int inited_stream_count;
  410. nut->avf= s;
  411. /* main header */
  412. pos=0;
  413. do{
  414. pos= find_startcode(bc, MAIN_STARTCODE, pos)+1;
  415. if (pos<0+1){
  416. av_log(s, AV_LOG_ERROR, "no main startcode found\n");
  417. return -1;
  418. }
  419. }while(decode_main_header(nut) < 0);
  420. /* stream headers */
  421. pos=0;
  422. for(inited_stream_count=0; inited_stream_count < s->nb_streams;){
  423. pos= find_startcode(bc, STREAM_STARTCODE, pos)+1;
  424. if (pos<0+1){
  425. av_log(s, AV_LOG_ERROR, "not all stream headers found\n");
  426. return -1;
  427. }
  428. if(decode_stream_header(nut) >= 0)
  429. inited_stream_count++;
  430. }
  431. /* info headers */
  432. pos=0;
  433. for(;;){
  434. uint64_t startcode= find_any_startcode(bc, pos);
  435. pos= url_ftell(bc);
  436. if(startcode==0){
  437. av_log(s, AV_LOG_ERROR, "EOF before video frames\n");
  438. return -1;
  439. }else if(startcode == SYNCPOINT_STARTCODE){
  440. nut->next_startcode= startcode;
  441. break;
  442. }else if(startcode != INFO_STARTCODE){
  443. continue;
  444. }
  445. decode_info_header(nut);
  446. }
  447. s->data_offset= pos-8;
  448. return 0;
  449. }
  450. static int decode_frame_header(NUTContext *nut, int *flags_ret, int64_t *pts, int *stream_id, int frame_code){
  451. AVFormatContext *s= nut->avf;
  452. ByteIOContext *bc = &s->pb;
  453. StreamContext *stc;
  454. int size, flags, size_mul, pts_delta, i, reserved_count;
  455. uint64_t tmp;
  456. if(url_ftell(bc) > nut->last_syncpoint_pos + nut->max_distance){
  457. 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);
  458. return -1;
  459. }
  460. flags = nut->frame_code[frame_code].flags;
  461. size_mul = nut->frame_code[frame_code].size_mul;
  462. size = nut->frame_code[frame_code].size_lsb;
  463. *stream_id = nut->frame_code[frame_code].stream_id;
  464. pts_delta = nut->frame_code[frame_code].pts_delta;
  465. reserved_count = nut->frame_code[frame_code].reserved_count;
  466. if(flags & FLAG_INVALID)
  467. return -1;
  468. if(flags & FLAG_CODED)
  469. flags ^= get_v(bc);
  470. if(flags & FLAG_STREAM_ID){
  471. GET_V(*stream_id, tmp < s->nb_streams)
  472. }
  473. stc= &nut->stream[*stream_id];
  474. if(flags&FLAG_CODED_PTS){
  475. int coded_pts= get_v(bc);
  476. //FIXME check last_pts validity?
  477. if(coded_pts < (1<<stc->msb_pts_shift)){
  478. *pts=lsb2full(stc, coded_pts);
  479. }else
  480. *pts=coded_pts - (1<<stc->msb_pts_shift);
  481. }else
  482. *pts= stc->last_pts + pts_delta;
  483. if(flags&FLAG_SIZE_MSB){
  484. size += size_mul*get_v(bc);
  485. }
  486. if(flags&FLAG_RESERVED)
  487. reserved_count= get_v(bc);
  488. for(i=0; i<reserved_count; i++)
  489. get_v(bc);
  490. if(flags&FLAG_CHECKSUM){
  491. get_be32(bc); //FIXME check this
  492. }
  493. *flags_ret= flags;
  494. stc->last_pts= *pts;
  495. stc->last_key_frame= flags&FLAG_KEY; //FIXME change to last flags
  496. if(flags&FLAG_KEY){
  497. av_add_index_entry(
  498. s->streams[*stream_id],
  499. nut->last_syncpoint_pos,
  500. *pts,
  501. 0,
  502. 0,
  503. AVINDEX_KEYFRAME);
  504. }
  505. return size;
  506. }
  507. static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code){
  508. AVFormatContext *s= nut->avf;
  509. ByteIOContext *bc = &s->pb;
  510. int size, stream_id, flags, discard;
  511. int64_t pts, last_IP_pts;
  512. size= decode_frame_header(nut, &flags, &pts, &stream_id, frame_code);
  513. if(size < 0)
  514. return -1;
  515. discard= s->streams[ stream_id ]->discard;
  516. last_IP_pts= s->streams[ stream_id ]->last_IP_pts;
  517. if( (discard >= AVDISCARD_NONKEY && !(flags & FLAG_KEY))
  518. ||(discard >= AVDISCARD_BIDIR && last_IP_pts != AV_NOPTS_VALUE && last_IP_pts > pts)
  519. || discard >= AVDISCARD_ALL){
  520. url_fskip(bc, size);
  521. return 1;
  522. }
  523. av_get_packet(bc, pkt, size);
  524. pkt->stream_index = stream_id;
  525. if (flags & FLAG_KEY)
  526. pkt->flags |= PKT_FLAG_KEY;
  527. pkt->pts = pts;
  528. return 0;
  529. }
  530. static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
  531. {
  532. NUTContext *nut = s->priv_data;
  533. ByteIOContext *bc = &s->pb;
  534. int i, frame_code=0, ret, skip;
  535. for(;;){
  536. int64_t pos= url_ftell(bc);
  537. uint64_t tmp= nut->next_startcode;
  538. nut->next_startcode=0;
  539. if (url_feof(bc))
  540. return -1;
  541. if(tmp){
  542. pos-=8;
  543. }else{
  544. frame_code = get_byte(bc);
  545. if(frame_code == 'N'){
  546. tmp= frame_code;
  547. for(i=1; i<8; i++)
  548. tmp = (tmp<<8) + get_byte(bc);
  549. }
  550. }
  551. switch(tmp){
  552. case MAIN_STARTCODE:
  553. case STREAM_STARTCODE:
  554. case INDEX_STARTCODE:
  555. skip= get_packetheader(nut, bc, 0);
  556. url_fseek(bc, skip, SEEK_CUR);
  557. break;
  558. case INFO_STARTCODE:
  559. if(decode_info_header(nut)<0)
  560. goto resync;
  561. break;
  562. case SYNCPOINT_STARTCODE:
  563. if(decode_syncpoint(nut)<0)
  564. goto resync;
  565. frame_code = get_byte(bc);
  566. case 0:
  567. ret= decode_frame(nut, pkt, frame_code);
  568. if(ret==0)
  569. return 0;
  570. else if(ret==1) //ok but discard packet
  571. break;
  572. default:
  573. resync:
  574. av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", pos);
  575. tmp= find_any_startcode(bc, pos+1);
  576. if(tmp==0)
  577. return -1;
  578. av_log(s, AV_LOG_DEBUG, "sync\n");
  579. nut->next_startcode= tmp;
  580. }
  581. }
  582. }
  583. static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index, int64_t *pos_arg, int64_t pos_limit){
  584. NUTContext *nut = s->priv_data;
  585. ByteIOContext *bc = &s->pb;
  586. int64_t pos, pts;
  587. int frame_code, stream_id,size, flags;
  588. av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n", stream_index, *pos_arg, pos_limit);
  589. pos= *pos_arg;
  590. resync:
  591. do{
  592. pos= find_startcode(bc, SYNCPOINT_STARTCODE, pos)+1;
  593. if(pos < 1){
  594. assert(nut->next_startcode == 0);
  595. av_log(s, AV_LOG_ERROR, "read_timestamp failed\n");
  596. return AV_NOPTS_VALUE;
  597. }
  598. }while(decode_syncpoint(nut) < 0);
  599. *pos_arg = pos-1;
  600. assert(nut->last_syncpoint_pos == *pos_arg);
  601. do{
  602. frame_code= get_byte(bc);
  603. if(frame_code == 'N'){
  604. pos= url_ftell(bc)-1;
  605. goto resync;
  606. }
  607. //FIXME consider pos_limit and eof
  608. size= decode_frame_header(nut, &flags, &pts, &stream_id, frame_code);
  609. if(size < 0)
  610. goto resync;
  611. url_fseek(bc, size, SEEK_CUR);
  612. }while(stream_id != stream_index || !(flags & FLAG_KEY));
  613. assert(nut->next_startcode == 0);
  614. av_log(s, AV_LOG_DEBUG, "read_timestamp success\n");
  615. return pts;
  616. }
  617. static int nut_read_close(AVFormatContext *s)
  618. {
  619. NUTContext *nut = s->priv_data;
  620. av_freep(&nut->time_base);
  621. av_freep(&nut->stream);
  622. return 0;
  623. }
  624. #ifdef CONFIG_NUT_DEMUXER
  625. AVInputFormat nut_demuxer = {
  626. "nut",
  627. "nut format",
  628. sizeof(NUTContext),
  629. nut_probe,
  630. nut_read_header,
  631. nut_read_packet,
  632. nut_read_close,
  633. NULL,
  634. nut_read_timestamp,
  635. .extensions = "nut",
  636. };
  637. #endif