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.

870 lines
26KB

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