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.

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