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.

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