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.

883 lines
27KB

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