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.

834 lines
24KB

  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, 0);
  95. // nut->packet_start[2] = start;
  96. // nut->written_packet_size= size;
  97. return size;
  98. }
  99. static uint64_t find_any_startcode(ByteIOContext *bc, int64_t pos){
  100. uint64_t state=0;
  101. if(pos >= 0)
  102. 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
  103. while(!url_feof(bc)){
  104. state= (state<<8) | get_byte(bc);
  105. if((state>>56) != 'N')
  106. continue;
  107. switch(state){
  108. case MAIN_STARTCODE:
  109. case STREAM_STARTCODE:
  110. case SYNCPOINT_STARTCODE:
  111. case INFO_STARTCODE:
  112. case INDEX_STARTCODE:
  113. return state;
  114. }
  115. }
  116. return 0;
  117. }
  118. /**
  119. * find the given startcode.
  120. * @param code the startcode
  121. * @param pos the start position of the search, or -1 if the current position
  122. * @returns the position of the startcode or -1 if not found
  123. */
  124. static int64_t find_startcode(ByteIOContext *bc, uint64_t code, int64_t pos){
  125. for(;;){
  126. uint64_t startcode= find_any_startcode(bc, pos);
  127. if(startcode == code)
  128. return url_ftell(bc) - 8;
  129. else if(startcode == 0)
  130. return -1;
  131. pos=-1;
  132. }
  133. }
  134. static int64_t lsb2full(StreamContext *stream, int64_t lsb){
  135. int64_t mask = (1<<stream->msb_pts_shift)-1;
  136. int64_t delta= stream->last_pts - mask/2;
  137. return ((lsb - delta)&mask) + delta;
  138. }
  139. static int nut_probe(AVProbeData *p){
  140. int i;
  141. uint64_t code= 0;
  142. for (i = 0; i < p->buf_size; i++) {
  143. code = (code << 8) | p->buf[i];
  144. if (code == MAIN_STARTCODE)
  145. return AVPROBE_SCORE_MAX;
  146. }
  147. return 0;
  148. }
  149. #define GET_V(dst, check) \
  150. tmp= get_v(bc);\
  151. if(!(check)){\
  152. av_log(s, AV_LOG_ERROR, "Error " #dst " is (%"PRId64")\n", tmp);\
  153. return -1;\
  154. }\
  155. dst= tmp;
  156. static int skip_reserved(ByteIOContext *bc, int64_t pos){
  157. pos -= url_ftell(bc);
  158. av_log(NULL, AV_LOG_ERROR, "skip %d\n", (int)pos);
  159. if(pos<0){
  160. url_fseek(bc, pos, SEEK_CUR);
  161. return -1;
  162. }else{
  163. while(pos--)
  164. get_byte(bc);
  165. return 0;
  166. }
  167. }
  168. static int decode_main_header(NUTContext *nut){
  169. AVFormatContext *s= nut->avf;
  170. ByteIOContext *bc = &s->pb;
  171. uint64_t tmp, end;
  172. unsigned int stream_count;
  173. int i, j, tmp_stream, tmp_mul, tmp_pts, tmp_size, count, tmp_res;
  174. end= get_packetheader(nut, bc, 1);
  175. end += url_ftell(bc);
  176. GET_V(tmp , tmp >=2 && tmp <= 3)
  177. GET_V(stream_count , tmp > 0 && tmp <=MAX_STREAMS)
  178. nut->max_distance = get_v(bc);
  179. if(nut->max_distance > 65536){
  180. av_log(s, AV_LOG_DEBUG, "max_distance %d\n", nut->max_distance);
  181. nut->max_distance= 65536;
  182. }
  183. GET_V(nut->time_base_count, tmp>0 && tmp<INT_MAX / sizeof(AVRational))
  184. nut->time_base= av_malloc(nut->time_base_count * sizeof(AVRational));
  185. for(i=0; i<nut->time_base_count; i++){
  186. GET_V(nut->time_base[i].num, tmp>0 && tmp<(1ULL<<31))
  187. GET_V(nut->time_base[i].den, tmp>0 && tmp<(1ULL<<31))
  188. if(ff_gcd(nut->time_base[i].num, nut->time_base[i].den) != 1){
  189. av_log(s, AV_LOG_ERROR, "time base invalid\n");
  190. return -1;
  191. }
  192. }
  193. tmp_pts=0;
  194. tmp_mul=1;
  195. tmp_stream=0;
  196. for(i=0; i<256;){
  197. int tmp_flags = get_v(bc);
  198. int tmp_fields= get_v(bc);
  199. if(tmp_fields>0) tmp_pts = get_s(bc);
  200. if(tmp_fields>1) tmp_mul = get_v(bc);
  201. if(tmp_fields>2) tmp_stream= get_v(bc);
  202. if(tmp_fields>3) tmp_size = get_v(bc);
  203. else tmp_size = 0;
  204. if(tmp_fields>4) tmp_res = get_v(bc);
  205. else tmp_res = 0;
  206. if(tmp_fields>5) count = get_v(bc);
  207. else count = tmp_mul - tmp_size;
  208. while(tmp_fields-- > 6)
  209. get_v(bc);
  210. if(count == 0 || i+count > 256){
  211. av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
  212. return -1;
  213. }
  214. if(tmp_stream >= stream_count){
  215. av_log(s, AV_LOG_ERROR, "illegal stream number\n");
  216. return -1;
  217. }
  218. for(j=0; j<count; j++,i++){
  219. if (i == 'N') {
  220. nut->frame_code[i].flags= FLAG_INVALID;
  221. j--;
  222. continue;
  223. }
  224. nut->frame_code[i].flags = tmp_flags ;
  225. nut->frame_code[i].pts_delta = tmp_pts ;
  226. nut->frame_code[i].stream_id = tmp_stream;
  227. nut->frame_code[i].size_mul = tmp_mul ;
  228. nut->frame_code[i].size_lsb = tmp_size+j;
  229. nut->frame_code[i].reserved_count = tmp_res ;
  230. }
  231. }
  232. assert(nut->frame_code['N'].flags == FLAG_INVALID);
  233. if(skip_reserved(bc, end) || get_checksum(bc)){
  234. av_log(s, AV_LOG_ERROR, "Main header checksum mismatch\n");
  235. return -1;
  236. }
  237. nut->stream = av_mallocz(sizeof(StreamContext)*stream_count);
  238. for(i=0; i<stream_count; i++){
  239. av_new_stream(s, i);
  240. }
  241. return 0;
  242. }
  243. static int decode_stream_header(NUTContext *nut){
  244. AVFormatContext *s= nut->avf;
  245. ByteIOContext *bc = &s->pb;
  246. StreamContext *stc;
  247. int class, nom, denom, stream_id;
  248. uint64_t tmp, end;
  249. AVStream *st;
  250. end= get_packetheader(nut, bc, 1);
  251. end += url_ftell(bc);
  252. GET_V(stream_id, tmp < s->nb_streams && !nut->stream[tmp].time_base.num);
  253. stc= &nut->stream[stream_id];
  254. st = s->streams[stream_id];
  255. if (!st)
  256. return AVERROR_NOMEM;
  257. class = get_v(bc);
  258. tmp = get_fourcc(bc);
  259. st->codec->codec_tag= tmp;
  260. switch(class)
  261. {
  262. case 0:
  263. st->codec->codec_type = CODEC_TYPE_VIDEO;
  264. st->codec->codec_id = codec_get_bmp_id(tmp);
  265. if (st->codec->codec_id == CODEC_ID_NONE)
  266. av_log(s, AV_LOG_ERROR, "Unknown codec?!\n");
  267. break;
  268. case 1:
  269. st->codec->codec_type = CODEC_TYPE_AUDIO;
  270. st->codec->codec_id = codec_get_wav_id(tmp);
  271. if (st->codec->codec_id == CODEC_ID_NONE)
  272. av_log(s, AV_LOG_ERROR, "Unknown codec?!\n");
  273. break;
  274. case 2:
  275. // st->codec->codec_type = CODEC_TYPE_TEXT;
  276. // break;
  277. case 3:
  278. st->codec->codec_type = CODEC_TYPE_DATA;
  279. break;
  280. default:
  281. av_log(s, AV_LOG_ERROR, "Unknown stream class (%d)\n", class);
  282. return -1;
  283. }
  284. GET_V(stc->time_base_id , tmp < nut->time_base_count);
  285. GET_V(stc->msb_pts_shift , tmp < 16);
  286. stc->max_pts_distance= get_v(bc);
  287. GET_V(stc->decode_delay , tmp < 1000); //sanity limit, raise this if moors law is true
  288. st->codec->has_b_frames= stc->decode_delay;
  289. get_v(bc); //stream flags
  290. GET_V(st->codec->extradata_size, tmp < (1<<30));
  291. if(st->codec->extradata_size){
  292. st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  293. get_buffer(bc, st->codec->extradata, st->codec->extradata_size);
  294. }
  295. if (st->codec->codec_type == CODEC_TYPE_VIDEO){
  296. GET_V(st->codec->width , tmp > 0)
  297. GET_V(st->codec->height, tmp > 0)
  298. st->codec->sample_aspect_ratio.num= get_v(bc);
  299. st->codec->sample_aspect_ratio.den= get_v(bc);
  300. if((!st->codec->sample_aspect_ratio.num) != (!st->codec->sample_aspect_ratio.den)){
  301. av_log(s, AV_LOG_ERROR, "invalid aspect ratio\n");
  302. return -1;
  303. }
  304. get_v(bc); /* csp type */
  305. }else if (st->codec->codec_type == CODEC_TYPE_AUDIO){
  306. GET_V(st->codec->sample_rate , tmp > 0)
  307. tmp= get_v(bc); // samplerate_den
  308. if(tmp > st->codec->sample_rate){
  309. av_log(s, AV_LOG_ERROR, "bleh, libnut muxed this ;)\n");
  310. st->codec->sample_rate= tmp;
  311. }
  312. GET_V(st->codec->channels, tmp > 0)
  313. }
  314. if(skip_reserved(bc, end) || get_checksum(bc)){
  315. av_log(s, AV_LOG_ERROR, "Stream header %d checksum mismatch\n", stream_id);
  316. return -1;
  317. }
  318. stc->time_base= nut->time_base[stc->time_base_id];
  319. av_set_pts_info(s->streams[stream_id], 63, stc->time_base.num, stc->time_base.den);
  320. return 0;
  321. }
  322. static int decode_info_header(NUTContext *nut){
  323. AVFormatContext *s= nut->avf;
  324. ByteIOContext *bc = &s->pb;
  325. uint64_t tmp;
  326. unsigned int stream_id_plus1, chapter_start, chapter_len, count;
  327. int chapter_id, i;
  328. int64_t value, end;
  329. char name[256], str_value[1024], type_str[256], *type= type_str;
  330. end= get_packetheader(nut, bc, 1);
  331. end += url_ftell(bc);
  332. GET_V(stream_id_plus1, tmp <= s->nb_streams)
  333. chapter_id = get_s(bc);
  334. chapter_start= get_v(bc);
  335. chapter_len = get_v(bc);
  336. count = get_v(bc);
  337. for(i=0; i<count; i++){
  338. get_str(bc, name, sizeof(name));
  339. value= get_s(bc);
  340. if(value == -1){
  341. type= "UTF-8";
  342. get_str(bc, str_value, sizeof(str_value));
  343. }else if(value == -2){
  344. get_str(bc, type, sizeof(type));
  345. get_str(bc, str_value, sizeof(str_value));
  346. }else if(value == -3){
  347. type= "s";
  348. value= get_s(bc);
  349. }else if(value == -4){
  350. type= "t";
  351. value= get_v(bc);
  352. }else if(value < -4){
  353. type= "r";
  354. get_s(bc);
  355. }else{
  356. type= "v";
  357. }
  358. if(chapter_id==0 && !strcmp(type, "UTF-8")){
  359. if (!strcmp(name, "Author"))
  360. pstrcpy(s->author , sizeof(s->author) , str_value);
  361. else if(!strcmp(name, "Title"))
  362. pstrcpy(s->title , sizeof(s->title) , str_value);
  363. else if(!strcmp(name, "Copyright"))
  364. pstrcpy(s->copyright, sizeof(s->copyright), str_value);
  365. else if(!strcmp(name, "Description"))
  366. pstrcpy(s->comment , sizeof(s->comment) , str_value);
  367. }
  368. }
  369. if(skip_reserved(bc, end) || get_checksum(bc)){
  370. av_log(s, AV_LOG_ERROR, "Info header checksum mismatch\n");
  371. return -1;
  372. }
  373. return 0;
  374. }
  375. static int decode_syncpoint(NUTContext *nut){
  376. AVFormatContext *s= nut->avf;
  377. ByteIOContext *bc = &s->pb;
  378. int64_t end;
  379. uint64_t tmp;
  380. int i;
  381. AVRational time_base;
  382. nut->last_syncpoint_pos= url_ftell(bc)-8;
  383. end= get_packetheader(nut, bc, 1);
  384. end += url_ftell(bc);
  385. tmp= get_v(bc);
  386. get_v(bc); //back_ptr_div16
  387. time_base= nut->time_base[tmp % nut->time_base_count];
  388. for(i=0; i<s->nb_streams; i++){
  389. nut->stream[i].last_pts= av_rescale_rnd(
  390. tmp / nut->time_base_count,
  391. time_base.num * (int64_t)nut->stream[i].time_base.den,
  392. time_base.den * (int64_t)nut->stream[i].time_base.num,
  393. AV_ROUND_DOWN);
  394. //last_key_frame ?
  395. }
  396. //FIXME put this in a reset func maybe
  397. if(skip_reserved(bc, end) || get_checksum(bc)){
  398. av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n");
  399. return -1;
  400. }
  401. return 0;
  402. }
  403. static int find_and_decode_index(NUTContext *nut){
  404. AVFormatContext *s= nut->avf;
  405. ByteIOContext *bc = &s->pb;
  406. uint64_t tmp, end;
  407. int i, j, syncpoint_count;
  408. int64_t filesize= url_fsize(bc);
  409. int64_t *syncpoints;
  410. int8_t *has_keyframe;
  411. url_fseek(bc, filesize-12, SEEK_SET);
  412. url_fseek(bc, filesize-get_be64(bc), SEEK_SET);
  413. if(get_be64(bc) != INDEX_STARTCODE){
  414. av_log(s, AV_LOG_ERROR, "no index at the end\n");
  415. return -1;
  416. }
  417. end= get_packetheader(nut, bc, 1);
  418. end += url_ftell(bc);
  419. get_v(bc); //max_pts
  420. GET_V(syncpoint_count, tmp < INT_MAX/8 && tmp > 0)
  421. syncpoints= av_malloc(sizeof(int64_t)*syncpoint_count);
  422. has_keyframe= av_malloc(sizeof(int8_t)*(syncpoint_count+1));
  423. for(i=0; i<syncpoint_count; i++){
  424. GET_V(syncpoints[i], tmp>0)
  425. if(i)
  426. syncpoints[i] += syncpoints[i-1];
  427. }
  428. for(i=0; i<s->nb_streams; i++){
  429. int64_t last_pts= -1;
  430. for(j=0; j<syncpoint_count;){
  431. uint64_t x= get_v(bc);
  432. int type= x&1;
  433. int n= j;
  434. x>>=1;
  435. if(type){
  436. int flag= x&1;
  437. x>>=1;
  438. if(n+x >= syncpoint_count + 1){
  439. av_log(s, AV_LOG_ERROR, "index overflow A\n");
  440. return -1;
  441. }
  442. while(x--)
  443. has_keyframe[n++]= flag;
  444. has_keyframe[n++]= !flag;
  445. }else{
  446. while(x != 1){
  447. if(n>=syncpoint_count + 1){
  448. av_log(s, AV_LOG_ERROR, "index overflow B\n");
  449. return -1;
  450. }
  451. has_keyframe[n++]= x&1;
  452. x>>=1;
  453. }
  454. }
  455. if(has_keyframe[0]){
  456. av_log(s, AV_LOG_ERROR, "keyframe before first syncpoint in index\n");
  457. return -1;
  458. }
  459. assert(n<=syncpoint_count+1);
  460. for(; j<n; j++){
  461. if(has_keyframe[j]){
  462. uint64_t B, A= get_v(bc);
  463. if(!A){
  464. A= get_v(bc);
  465. B= get_v(bc);
  466. //eor_pts[j][i] = last_pts + A + B
  467. }else
  468. B= 0;
  469. av_add_index_entry(
  470. s->streams[i],
  471. 16*syncpoints[j-1],
  472. last_pts + A,
  473. 0,
  474. 0,
  475. AVINDEX_KEYFRAME);
  476. last_pts += A + B;
  477. }
  478. }
  479. }
  480. }
  481. if(skip_reserved(bc, end) || get_checksum(bc)){
  482. av_log(s, AV_LOG_ERROR, "Index checksum mismatch\n");
  483. return -1;
  484. }
  485. return 0;
  486. }
  487. static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
  488. {
  489. NUTContext *nut = s->priv_data;
  490. ByteIOContext *bc = &s->pb;
  491. int64_t pos;
  492. int inited_stream_count;
  493. nut->avf= s;
  494. /* main header */
  495. pos=0;
  496. do{
  497. pos= find_startcode(bc, MAIN_STARTCODE, pos)+1;
  498. if (pos<0+1){
  499. av_log(s, AV_LOG_ERROR, "no main startcode found\n");
  500. return -1;
  501. }
  502. }while(decode_main_header(nut) < 0);
  503. /* stream headers */
  504. pos=0;
  505. for(inited_stream_count=0; inited_stream_count < s->nb_streams;){
  506. pos= find_startcode(bc, STREAM_STARTCODE, pos)+1;
  507. if (pos<0+1){
  508. av_log(s, AV_LOG_ERROR, "not all stream headers found\n");
  509. return -1;
  510. }
  511. if(decode_stream_header(nut) >= 0)
  512. inited_stream_count++;
  513. }
  514. /* info headers */
  515. pos=0;
  516. for(;;){
  517. uint64_t startcode= find_any_startcode(bc, pos);
  518. pos= url_ftell(bc);
  519. if(startcode==0){
  520. av_log(s, AV_LOG_ERROR, "EOF before video frames\n");
  521. return -1;
  522. }else if(startcode == SYNCPOINT_STARTCODE){
  523. nut->next_startcode= startcode;
  524. break;
  525. }else if(startcode != INFO_STARTCODE){
  526. continue;
  527. }
  528. decode_info_header(nut);
  529. }
  530. s->data_offset= pos-8;
  531. if(0 &&!url_is_streamed(bc)){
  532. int64_t orig_pos= url_ftell(bc);
  533. find_and_decode_index(nut);
  534. url_fseek(bc, orig_pos, SEEK_SET);
  535. }
  536. assert(nut->next_startcode == SYNCPOINT_STARTCODE);
  537. return 0;
  538. }
  539. static int decode_frame_header(NUTContext *nut, int *flags_ret, int64_t *pts, int *stream_id, int frame_code){
  540. AVFormatContext *s= nut->avf;
  541. ByteIOContext *bc = &s->pb;
  542. StreamContext *stc;
  543. int size, flags, size_mul, pts_delta, i, reserved_count;
  544. uint64_t tmp;
  545. if(url_ftell(bc) > nut->last_syncpoint_pos + nut->max_distance){
  546. 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);
  547. return -1;
  548. }
  549. flags = nut->frame_code[frame_code].flags;
  550. size_mul = nut->frame_code[frame_code].size_mul;
  551. size = nut->frame_code[frame_code].size_lsb;
  552. *stream_id = nut->frame_code[frame_code].stream_id;
  553. pts_delta = nut->frame_code[frame_code].pts_delta;
  554. reserved_count = nut->frame_code[frame_code].reserved_count;
  555. if(flags & FLAG_INVALID)
  556. return -1;
  557. if(flags & FLAG_CODED)
  558. flags ^= get_v(bc);
  559. if(flags & FLAG_STREAM_ID){
  560. GET_V(*stream_id, tmp < s->nb_streams)
  561. }
  562. stc= &nut->stream[*stream_id];
  563. if(flags&FLAG_CODED_PTS){
  564. int coded_pts= get_v(bc);
  565. //FIXME check last_pts validity?
  566. if(coded_pts < (1<<stc->msb_pts_shift)){
  567. *pts=lsb2full(stc, coded_pts);
  568. }else
  569. *pts=coded_pts - (1<<stc->msb_pts_shift);
  570. }else
  571. *pts= stc->last_pts + pts_delta;
  572. if(flags&FLAG_SIZE_MSB){
  573. size += size_mul*get_v(bc);
  574. }
  575. if(flags&FLAG_RESERVED)
  576. reserved_count= get_v(bc);
  577. for(i=0; i<reserved_count; i++)
  578. get_v(bc);
  579. if(flags&FLAG_CHECKSUM){
  580. get_be32(bc); //FIXME check this
  581. }
  582. *flags_ret= flags;
  583. stc->last_pts= *pts;
  584. stc->last_key_frame= flags&FLAG_KEY; //FIXME change to last flags
  585. if(flags&FLAG_KEY){
  586. av_add_index_entry(
  587. s->streams[*stream_id],
  588. nut->last_syncpoint_pos,
  589. *pts,
  590. 0,
  591. 0,
  592. AVINDEX_KEYFRAME);
  593. }
  594. return size;
  595. }
  596. static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code){
  597. AVFormatContext *s= nut->avf;
  598. ByteIOContext *bc = &s->pb;
  599. int size, stream_id, flags, discard;
  600. int64_t pts, last_IP_pts;
  601. size= decode_frame_header(nut, &flags, &pts, &stream_id, frame_code);
  602. if(size < 0)
  603. return -1;
  604. discard= s->streams[ stream_id ]->discard;
  605. last_IP_pts= s->streams[ stream_id ]->last_IP_pts;
  606. if( (discard >= AVDISCARD_NONKEY && !(flags & FLAG_KEY))
  607. ||(discard >= AVDISCARD_BIDIR && last_IP_pts != AV_NOPTS_VALUE && last_IP_pts > pts)
  608. || discard >= AVDISCARD_ALL){
  609. url_fskip(bc, size);
  610. return 1;
  611. }
  612. av_get_packet(bc, pkt, size);
  613. pkt->stream_index = stream_id;
  614. if (flags & FLAG_KEY)
  615. pkt->flags |= PKT_FLAG_KEY;
  616. pkt->pts = pts;
  617. return 0;
  618. }
  619. static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
  620. {
  621. NUTContext *nut = s->priv_data;
  622. ByteIOContext *bc = &s->pb;
  623. int i, frame_code=0, ret, skip;
  624. for(;;){
  625. int64_t pos= url_ftell(bc);
  626. uint64_t tmp= nut->next_startcode;
  627. nut->next_startcode=0;
  628. if (url_feof(bc))
  629. return -1;
  630. if(tmp){
  631. pos-=8;
  632. }else{
  633. frame_code = get_byte(bc);
  634. if(frame_code == 'N'){
  635. tmp= frame_code;
  636. for(i=1; i<8; i++)
  637. tmp = (tmp<<8) + get_byte(bc);
  638. }
  639. }
  640. switch(tmp){
  641. case MAIN_STARTCODE:
  642. case STREAM_STARTCODE:
  643. case INDEX_STARTCODE:
  644. skip= get_packetheader(nut, bc, 0);
  645. url_fseek(bc, skip, SEEK_CUR);
  646. break;
  647. case INFO_STARTCODE:
  648. if(decode_info_header(nut)<0)
  649. goto resync;
  650. break;
  651. case SYNCPOINT_STARTCODE:
  652. if(decode_syncpoint(nut)<0)
  653. goto resync;
  654. frame_code = get_byte(bc);
  655. case 0:
  656. ret= decode_frame(nut, pkt, frame_code);
  657. if(ret==0)
  658. return 0;
  659. else if(ret==1) //ok but discard packet
  660. break;
  661. default:
  662. resync:
  663. av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", pos);
  664. tmp= find_any_startcode(bc, pos+1);
  665. if(tmp==0)
  666. return -1;
  667. av_log(s, AV_LOG_DEBUG, "sync\n");
  668. nut->next_startcode= tmp;
  669. }
  670. }
  671. }
  672. static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index, int64_t *pos_arg, int64_t pos_limit){
  673. NUTContext *nut = s->priv_data;
  674. ByteIOContext *bc = &s->pb;
  675. int64_t pos, pts;
  676. int frame_code, stream_id,size, flags;
  677. av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n", stream_index, *pos_arg, pos_limit);
  678. pos= *pos_arg;
  679. resync:
  680. do{
  681. pos= find_startcode(bc, SYNCPOINT_STARTCODE, pos)+1;
  682. if(pos < 1){
  683. assert(nut->next_startcode == 0);
  684. av_log(s, AV_LOG_ERROR, "read_timestamp failed\n");
  685. return AV_NOPTS_VALUE;
  686. }
  687. }while(decode_syncpoint(nut) < 0);
  688. *pos_arg = pos-1;
  689. assert(nut->last_syncpoint_pos == *pos_arg);
  690. do{
  691. frame_code= get_byte(bc);
  692. if(frame_code == 'N'){
  693. pos= url_ftell(bc)-1;
  694. goto resync;
  695. }
  696. //FIXME consider pos_limit and eof
  697. size= decode_frame_header(nut, &flags, &pts, &stream_id, frame_code);
  698. if(size < 0)
  699. goto resync;
  700. url_fseek(bc, size, SEEK_CUR);
  701. }while(stream_id != stream_index || !(flags & FLAG_KEY));
  702. assert(nut->next_startcode == 0);
  703. av_log(s, AV_LOG_DEBUG, "read_timestamp success\n");
  704. return pts;
  705. }
  706. static int nut_read_close(AVFormatContext *s)
  707. {
  708. NUTContext *nut = s->priv_data;
  709. av_freep(&nut->time_base);
  710. av_freep(&nut->stream);
  711. return 0;
  712. }
  713. #ifdef CONFIG_NUT_DEMUXER
  714. AVInputFormat nut_demuxer = {
  715. "nut",
  716. "nut format",
  717. sizeof(NUTContext),
  718. nut_probe,
  719. nut_read_header,
  720. nut_read_packet,
  721. nut_read_close,
  722. NULL,
  723. nut_read_timestamp,
  724. .extensions = "nut",
  725. };
  726. #endif