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.

901 lines
28KB

  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 int get_str(ByteIOContext *bc, char *string, unsigned int maxlen){
  28. unsigned int len= ff_get_v(bc);
  29. if(len && maxlen)
  30. get_buffer(bc, string, FFMIN(len, maxlen));
  31. while(len > maxlen){
  32. get_byte(bc);
  33. len--;
  34. }
  35. if(maxlen)
  36. string[FFMIN(len, maxlen-1)]= 0;
  37. if(maxlen == len)
  38. return -1;
  39. else
  40. return 0;
  41. }
  42. static int64_t get_s(ByteIOContext *bc){
  43. int64_t v = ff_get_v(bc) + 1;
  44. if (v&1) return -(v>>1);
  45. else return (v>>1);
  46. }
  47. static uint64_t get_fourcc(ByteIOContext *bc){
  48. unsigned int len= ff_get_v(bc);
  49. if (len==2) return get_le16(bc);
  50. else if(len==4) return get_le32(bc);
  51. else return -1;
  52. }
  53. #ifdef TRACE
  54. static inline uint64_t get_v_trace(ByteIOContext *bc, char *file, char *func, int line){
  55. uint64_t v= ff_get_v(bc);
  56. av_log(NULL, AV_LOG_DEBUG, "get_v %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
  57. return v;
  58. }
  59. static inline int64_t get_s_trace(ByteIOContext *bc, char *file, char *func, int line){
  60. int64_t v= get_s(bc);
  61. av_log(NULL, AV_LOG_DEBUG, "get_s %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
  62. return v;
  63. }
  64. static inline uint64_t get_vb_trace(ByteIOContext *bc, char *file, char *func, int line){
  65. uint64_t v= get_vb(bc);
  66. av_log(NULL, AV_LOG_DEBUG, "get_vb %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
  67. return v;
  68. }
  69. #define ff_get_v(bc) get_v_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  70. #define get_s(bc) get_s_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  71. #define get_vb(bc) get_vb_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  72. #endif
  73. static int get_packetheader(NUTContext *nut, ByteIOContext *bc, int calculate_checksum, uint64_t startcode)
  74. {
  75. int64_t size;
  76. // start= url_ftell(bc) - 8;
  77. startcode= be2me_64(startcode);
  78. startcode= ff_crc04C11DB7_update(0, &startcode, 8);
  79. init_checksum(bc, ff_crc04C11DB7_update, startcode);
  80. size= ff_get_v(bc);
  81. if(size > 4096)
  82. get_be32(bc);
  83. if(get_checksum(bc) && size > 4096)
  84. return -1;
  85. init_checksum(bc, calculate_checksum ? ff_crc04C11DB7_update : NULL, 0);
  86. return size;
  87. }
  88. static uint64_t find_any_startcode(ByteIOContext *bc, int64_t pos){
  89. uint64_t state=0;
  90. if(pos >= 0)
  91. 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
  92. while(!url_feof(bc)){
  93. state= (state<<8) | get_byte(bc);
  94. if((state>>56) != 'N')
  95. continue;
  96. switch(state){
  97. case MAIN_STARTCODE:
  98. case STREAM_STARTCODE:
  99. case SYNCPOINT_STARTCODE:
  100. case INFO_STARTCODE:
  101. case INDEX_STARTCODE:
  102. return state;
  103. }
  104. }
  105. return 0;
  106. }
  107. /**
  108. * Find the given startcode.
  109. * @param code the startcode
  110. * @param pos the start position of the search, or -1 if the current position
  111. * @returns the position of the startcode or -1 if not found
  112. */
  113. static int64_t find_startcode(ByteIOContext *bc, uint64_t code, int64_t pos){
  114. for(;;){
  115. uint64_t startcode= find_any_startcode(bc, pos);
  116. if(startcode == code)
  117. return url_ftell(bc) - 8;
  118. else if(startcode == 0)
  119. return -1;
  120. pos=-1;
  121. }
  122. }
  123. static int nut_probe(AVProbeData *p){
  124. int i;
  125. uint64_t code= 0;
  126. for (i = 0; i < p->buf_size; i++) {
  127. code = (code << 8) | p->buf[i];
  128. if (code == MAIN_STARTCODE)
  129. return AVPROBE_SCORE_MAX;
  130. }
  131. return 0;
  132. }
  133. #define GET_V(dst, check) \
  134. tmp= ff_get_v(bc);\
  135. if(!(check)){\
  136. av_log(s, AV_LOG_ERROR, "Error " #dst " is (%"PRId64")\n", tmp);\
  137. return -1;\
  138. }\
  139. dst= tmp;
  140. static int skip_reserved(ByteIOContext *bc, int64_t pos){
  141. pos -= url_ftell(bc);
  142. if(pos<0){
  143. url_fseek(bc, pos, SEEK_CUR);
  144. return -1;
  145. }else{
  146. while(pos--)
  147. get_byte(bc);
  148. return 0;
  149. }
  150. }
  151. static int decode_main_header(NUTContext *nut){
  152. AVFormatContext *s= nut->avf;
  153. ByteIOContext *bc = s->pb;
  154. uint64_t tmp, end;
  155. unsigned int stream_count;
  156. int i, j, tmp_stream, tmp_mul, tmp_pts, tmp_size, count, tmp_res, tmp_head_idx;
  157. int64_t tmp_match;
  158. end= get_packetheader(nut, bc, 1, MAIN_STARTCODE);
  159. end += url_ftell(bc);
  160. GET_V(tmp , tmp >=2 && tmp <= 3)
  161. GET_V(stream_count , tmp > 0 && tmp <=MAX_STREAMS)
  162. nut->max_distance = ff_get_v(bc);
  163. if(nut->max_distance > 65536){
  164. av_log(s, AV_LOG_DEBUG, "max_distance %d\n", nut->max_distance);
  165. nut->max_distance= 65536;
  166. }
  167. GET_V(nut->time_base_count, tmp>0 && tmp<INT_MAX / sizeof(AVRational))
  168. nut->time_base= av_malloc(nut->time_base_count * sizeof(AVRational));
  169. for(i=0; i<nut->time_base_count; i++){
  170. GET_V(nut->time_base[i].num, tmp>0 && tmp<(1ULL<<31))
  171. GET_V(nut->time_base[i].den, tmp>0 && tmp<(1ULL<<31))
  172. if(ff_gcd(nut->time_base[i].num, nut->time_base[i].den) != 1){
  173. av_log(s, AV_LOG_ERROR, "time base invalid\n");
  174. return -1;
  175. }
  176. }
  177. tmp_pts=0;
  178. tmp_mul=1;
  179. tmp_stream=0;
  180. tmp_match= 1-(1LL<<62);
  181. tmp_head_idx= 0;
  182. for(i=0; i<256;){
  183. int tmp_flags = ff_get_v(bc);
  184. int tmp_fields= ff_get_v(bc);
  185. if(tmp_fields>0) tmp_pts = get_s(bc);
  186. if(tmp_fields>1) tmp_mul = ff_get_v(bc);
  187. if(tmp_fields>2) tmp_stream= ff_get_v(bc);
  188. if(tmp_fields>3) tmp_size = ff_get_v(bc);
  189. else tmp_size = 0;
  190. if(tmp_fields>4) tmp_res = ff_get_v(bc);
  191. else tmp_res = 0;
  192. if(tmp_fields>5) count = ff_get_v(bc);
  193. else count = tmp_mul - tmp_size;
  194. if(tmp_fields>6) tmp_match = get_s(bc);
  195. if(tmp_fields>7) tmp_head_idx= ff_get_v(bc);
  196. while(tmp_fields-- > 8)
  197. ff_get_v(bc);
  198. if(count == 0 || i+count > 256){
  199. av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
  200. return -1;
  201. }
  202. if(tmp_stream >= stream_count){
  203. av_log(s, AV_LOG_ERROR, "illegal stream number\n");
  204. return -1;
  205. }
  206. for(j=0; j<count; j++,i++){
  207. if (i == 'N') {
  208. nut->frame_code[i].flags= FLAG_INVALID;
  209. j--;
  210. continue;
  211. }
  212. nut->frame_code[i].flags = tmp_flags ;
  213. nut->frame_code[i].pts_delta = tmp_pts ;
  214. nut->frame_code[i].stream_id = tmp_stream;
  215. nut->frame_code[i].size_mul = tmp_mul ;
  216. nut->frame_code[i].size_lsb = tmp_size+j;
  217. nut->frame_code[i].reserved_count = tmp_res ;
  218. nut->frame_code[i].header_idx = tmp_head_idx;
  219. }
  220. }
  221. assert(nut->frame_code['N'].flags == FLAG_INVALID);
  222. if(end > url_ftell(bc) + 4){
  223. int rem= 1024;
  224. GET_V(nut->header_count, tmp<128U)
  225. nut->header_count++;
  226. for(i=1; i<nut->header_count; i++){
  227. GET_V(nut->header_len[i], tmp>0 && tmp<256);
  228. rem -= nut->header_len[i];
  229. if(rem < 0){
  230. av_log(s, AV_LOG_ERROR, "invalid elision header\n");
  231. return -1;
  232. }
  233. nut->header[i]= av_malloc(nut->header_len[i]);
  234. get_buffer(bc, nut->header[i], nut->header_len[i]);
  235. }
  236. assert(nut->header_len[0]==0);
  237. }
  238. if(skip_reserved(bc, end) || get_checksum(bc)){
  239. av_log(s, AV_LOG_ERROR, "main header checksum mismatch\n");
  240. return -1;
  241. }
  242. nut->stream = av_mallocz(sizeof(StreamContext)*stream_count);
  243. for(i=0; i<stream_count; i++){
  244. av_new_stream(s, i);
  245. }
  246. return 0;
  247. }
  248. static int decode_stream_header(NUTContext *nut){
  249. AVFormatContext *s= nut->avf;
  250. ByteIOContext *bc = s->pb;
  251. StreamContext *stc;
  252. int class, stream_id;
  253. uint64_t tmp, end;
  254. AVStream *st;
  255. end= get_packetheader(nut, bc, 1, STREAM_STARTCODE);
  256. end += url_ftell(bc);
  257. GET_V(stream_id, tmp < s->nb_streams && !nut->stream[tmp].time_base);
  258. stc= &nut->stream[stream_id];
  259. st = s->streams[stream_id];
  260. if (!st)
  261. return AVERROR(ENOMEM);
  262. class = ff_get_v(bc);
  263. tmp = get_fourcc(bc);
  264. st->codec->codec_tag= tmp;
  265. switch(class)
  266. {
  267. case 0:
  268. st->codec->codec_type = CODEC_TYPE_VIDEO;
  269. st->codec->codec_id = codec_get_id(codec_bmp_tags, tmp);
  270. break;
  271. case 1:
  272. st->codec->codec_type = CODEC_TYPE_AUDIO;
  273. st->codec->codec_id = codec_get_id(codec_wav_tags, tmp);
  274. break;
  275. case 2:
  276. st->codec->codec_type = CODEC_TYPE_SUBTITLE;
  277. st->codec->codec_id = codec_get_id(ff_nut_subtitle_tags, tmp);
  278. break;
  279. case 3:
  280. st->codec->codec_type = CODEC_TYPE_DATA;
  281. break;
  282. default:
  283. av_log(s, AV_LOG_ERROR, "unknown stream class (%d)\n", class);
  284. return -1;
  285. }
  286. if(class<3 && st->codec->codec_id == CODEC_ID_NONE)
  287. av_log(s, AV_LOG_ERROR, "Unknown codec?!\n");
  288. GET_V(stc->time_base_id , tmp < nut->time_base_count);
  289. GET_V(stc->msb_pts_shift , tmp < 16);
  290. stc->max_pts_distance= ff_get_v(bc);
  291. GET_V(stc->decode_delay , tmp < 1000); //sanity limit, raise this if Moore's law is true
  292. st->codec->has_b_frames= stc->decode_delay;
  293. ff_get_v(bc); //stream flags
  294. GET_V(st->codec->extradata_size, tmp < (1<<30));
  295. if(st->codec->extradata_size){
  296. st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  297. get_buffer(bc, st->codec->extradata, st->codec->extradata_size);
  298. }
  299. if (st->codec->codec_type == CODEC_TYPE_VIDEO){
  300. GET_V(st->codec->width , tmp > 0)
  301. GET_V(st->codec->height, tmp > 0)
  302. st->codec->sample_aspect_ratio.num= ff_get_v(bc);
  303. st->codec->sample_aspect_ratio.den= ff_get_v(bc);
  304. if((!st->codec->sample_aspect_ratio.num) != (!st->codec->sample_aspect_ratio.den)){
  305. av_log(s, AV_LOG_ERROR, "invalid aspect ratio %d/%d\n", st->codec->sample_aspect_ratio.num, st->codec->sample_aspect_ratio.den);
  306. return -1;
  307. }
  308. ff_get_v(bc); /* csp type */
  309. }else if (st->codec->codec_type == CODEC_TYPE_AUDIO){
  310. GET_V(st->codec->sample_rate , tmp > 0)
  311. ff_get_v(bc); // samplerate_den
  312. GET_V(st->codec->channels, tmp > 0)
  313. }
  314. if(skip_reserved(bc, end) || get_checksum(bc)){
  315. av_log(s, AV_LOG_ERROR, "stream header %d checksum mismatch\n", stream_id);
  316. return -1;
  317. }
  318. stc->time_base= &nut->time_base[stc->time_base_id];
  319. av_set_pts_info(s->streams[stream_id], 63, stc->time_base->num, stc->time_base->den);
  320. return 0;
  321. }
  322. static void set_disposition_bits(AVFormatContext* avf, char* value, int stream_id){
  323. int flag = 0, i;
  324. for (i=0; ff_nut_dispositions[i].flag; ++i) {
  325. if (!strcmp(ff_nut_dispositions[i].str, value))
  326. flag = ff_nut_dispositions[i].flag;
  327. }
  328. if (!flag)
  329. av_log(avf, AV_LOG_INFO, "unknown disposition type '%s'\n", value);
  330. for (i = 0; i < avf->nb_streams; ++i)
  331. if (stream_id == i || stream_id == -1)
  332. avf->streams[i]->disposition |= flag;
  333. }
  334. static int decode_info_header(NUTContext *nut){
  335. AVFormatContext *s= nut->avf;
  336. ByteIOContext *bc = s->pb;
  337. uint64_t tmp;
  338. unsigned int stream_id_plus1, chapter_start, chapter_len, count;
  339. int chapter_id, i;
  340. int64_t value, end;
  341. char name[256], str_value[1024], type_str[256];
  342. const char *type;
  343. end= get_packetheader(nut, bc, 1, INFO_STARTCODE);
  344. end += url_ftell(bc);
  345. GET_V(stream_id_plus1, tmp <= s->nb_streams)
  346. chapter_id = get_s(bc);
  347. chapter_start= ff_get_v(bc);
  348. chapter_len = ff_get_v(bc);
  349. count = ff_get_v(bc);
  350. for(i=0; i<count; i++){
  351. get_str(bc, name, sizeof(name));
  352. value= get_s(bc);
  353. if(value == -1){
  354. type= "UTF-8";
  355. get_str(bc, str_value, sizeof(str_value));
  356. }else if(value == -2){
  357. get_str(bc, type_str, sizeof(type_str));
  358. type= type_str;
  359. get_str(bc, str_value, sizeof(str_value));
  360. }else if(value == -3){
  361. type= "s";
  362. value= get_s(bc);
  363. }else if(value == -4){
  364. type= "t";
  365. value= ff_get_v(bc);
  366. }else if(value < -4){
  367. type= "r";
  368. get_s(bc);
  369. }else{
  370. type= "v";
  371. }
  372. if (stream_id_plus1 < 0 || stream_id_plus1 > s->nb_streams) {
  373. av_log(s, AV_LOG_ERROR, "invalid stream id for info packet\n");
  374. continue;
  375. }
  376. if(chapter_id==0 && !strcmp(type, "UTF-8")){
  377. if (!strcmp(name, "Author"))
  378. av_strlcpy(s->author , str_value, sizeof(s->author));
  379. else if(!strcmp(name, "Title"))
  380. av_strlcpy(s->title , str_value, sizeof(s->title));
  381. else if(!strcmp(name, "Copyright"))
  382. av_strlcpy(s->copyright, str_value, sizeof(s->copyright));
  383. else if(!strcmp(name, "Description"))
  384. av_strlcpy(s->comment , str_value, sizeof(s->comment));
  385. else if(!strcmp(name, "Disposition"))
  386. set_disposition_bits(s, str_value, stream_id_plus1 - 1);
  387. }
  388. }
  389. if(skip_reserved(bc, end) || get_checksum(bc)){
  390. av_log(s, AV_LOG_ERROR, "info header checksum mismatch\n");
  391. return -1;
  392. }
  393. return 0;
  394. }
  395. static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr){
  396. AVFormatContext *s= nut->avf;
  397. ByteIOContext *bc = s->pb;
  398. int64_t end, tmp;
  399. nut->last_syncpoint_pos= url_ftell(bc)-8;
  400. end= get_packetheader(nut, bc, 1, SYNCPOINT_STARTCODE);
  401. end += url_ftell(bc);
  402. tmp= ff_get_v(bc);
  403. *back_ptr= nut->last_syncpoint_pos - 16*ff_get_v(bc);
  404. if(*back_ptr < 0)
  405. return -1;
  406. ff_nut_reset_ts(nut, nut->time_base[tmp % nut->time_base_count], tmp / nut->time_base_count);
  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. ff_nut_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, INDEX_STARTCODE);
  430. end += url_ftell(bc);
  431. ff_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= ff_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<syncpoint_count; j++){
  473. if(has_keyframe[j]){
  474. uint64_t B, A= ff_get_v(bc);
  475. if(!A){
  476. A= ff_get_v(bc);
  477. B= ff_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 initialized_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(initialized_stream_count=0; initialized_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. initialized_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, uint8_t *header_idx, 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 %"PRId64" > %"PRId64" + %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. *header_idx = nut->frame_code[frame_code].header_idx;
  568. if(flags & FLAG_INVALID)
  569. return -1;
  570. if(flags & FLAG_CODED)
  571. flags ^= ff_get_v(bc);
  572. if(flags & FLAG_STREAM_ID){
  573. GET_V(*stream_id, tmp < s->nb_streams)
  574. }
  575. stc= &nut->stream[*stream_id];
  576. if(flags&FLAG_CODED_PTS){
  577. int coded_pts= ff_get_v(bc);
  578. //FIXME check last_pts validity?
  579. if(coded_pts < (1<<stc->msb_pts_shift)){
  580. *pts=ff_lsb2full(stc, coded_pts);
  581. }else
  582. *pts=coded_pts - (1<<stc->msb_pts_shift);
  583. }else
  584. *pts= stc->last_pts + pts_delta;
  585. if(flags&FLAG_SIZE_MSB){
  586. size += size_mul*ff_get_v(bc);
  587. }
  588. if(flags&FLAG_MATCH_TIME)
  589. get_s(bc);
  590. if(flags&FLAG_HEADER_IDX)
  591. *header_idx= ff_get_v(bc);
  592. if(flags&FLAG_RESERVED)
  593. reserved_count= ff_get_v(bc);
  594. for(i=0; i<reserved_count; i++)
  595. ff_get_v(bc);
  596. if(*header_idx >= (unsigned)nut->header_count){
  597. av_log(s, AV_LOG_ERROR, "header_idx invalid\n");
  598. return -1;
  599. }
  600. if(size > 4096)
  601. *header_idx=0;
  602. size -= nut->header_len[*header_idx];
  603. if(flags&FLAG_CHECKSUM){
  604. get_be32(bc); //FIXME check this
  605. }else if(size > 2*nut->max_distance || FFABS(stc->last_pts - *pts) > stc->max_pts_distance){
  606. av_log(s, AV_LOG_ERROR, "frame size > 2max_distance and no checksum\n");
  607. return -1;
  608. }
  609. stc->last_pts= *pts;
  610. stc->last_flags= flags;
  611. return size;
  612. }
  613. static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code){
  614. AVFormatContext *s= nut->avf;
  615. ByteIOContext *bc = s->pb;
  616. int size, stream_id, discard;
  617. int64_t pts, last_IP_pts;
  618. StreamContext *stc;
  619. uint8_t header_idx;
  620. size= decode_frame_header(nut, &pts, &stream_id, &header_idx, frame_code);
  621. if(size < 0)
  622. return -1;
  623. stc= &nut->stream[stream_id];
  624. if (stc->last_flags & FLAG_KEY)
  625. stc->skip_until_key_frame=0;
  626. discard= s->streams[ stream_id ]->discard;
  627. last_IP_pts= s->streams[ stream_id ]->last_IP_pts;
  628. if( (discard >= AVDISCARD_NONKEY && !(stc->last_flags & FLAG_KEY))
  629. ||(discard >= AVDISCARD_BIDIR && last_IP_pts != AV_NOPTS_VALUE && last_IP_pts > pts)
  630. || discard >= AVDISCARD_ALL
  631. || stc->skip_until_key_frame){
  632. url_fskip(bc, size);
  633. return 1;
  634. }
  635. av_new_packet(pkt, size + nut->header_len[header_idx]);
  636. memcpy(pkt->data, nut->header[header_idx], nut->header_len[header_idx]);
  637. pkt->pos= url_ftell(bc); //FIXME
  638. get_buffer(bc, pkt->data + nut->header_len[header_idx], size);
  639. pkt->stream_index = stream_id;
  640. if (stc->last_flags & FLAG_KEY)
  641. pkt->flags |= PKT_FLAG_KEY;
  642. pkt->pts = pts;
  643. return 0;
  644. }
  645. static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
  646. {
  647. NUTContext *nut = s->priv_data;
  648. ByteIOContext *bc = s->pb;
  649. int i, frame_code=0, ret, skip;
  650. int64_t ts, back_ptr;
  651. for(;;){
  652. int64_t pos= url_ftell(bc);
  653. uint64_t tmp= nut->next_startcode;
  654. nut->next_startcode=0;
  655. if(tmp){
  656. pos-=8;
  657. }else{
  658. frame_code = get_byte(bc);
  659. if(url_feof(bc))
  660. return -1;
  661. if(frame_code == 'N'){
  662. tmp= frame_code;
  663. for(i=1; i<8; i++)
  664. tmp = (tmp<<8) + get_byte(bc);
  665. }
  666. }
  667. switch(tmp){
  668. case MAIN_STARTCODE:
  669. case STREAM_STARTCODE:
  670. case INDEX_STARTCODE:
  671. skip= get_packetheader(nut, bc, 0, tmp);
  672. url_fseek(bc, skip, SEEK_CUR);
  673. break;
  674. case INFO_STARTCODE:
  675. if(decode_info_header(nut)<0)
  676. goto resync;
  677. break;
  678. case SYNCPOINT_STARTCODE:
  679. if(decode_syncpoint(nut, &ts, &back_ptr)<0)
  680. goto resync;
  681. frame_code = get_byte(bc);
  682. case 0:
  683. ret= decode_frame(nut, pkt, frame_code);
  684. if(ret==0)
  685. return 0;
  686. else if(ret==1) //ok but discard packet
  687. break;
  688. default:
  689. resync:
  690. av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", pos);
  691. tmp= find_any_startcode(bc, nut->last_syncpoint_pos+1);
  692. if(tmp==0)
  693. return -1;
  694. av_log(s, AV_LOG_DEBUG, "sync\n");
  695. nut->next_startcode= tmp;
  696. }
  697. }
  698. }
  699. static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index, int64_t *pos_arg, int64_t pos_limit){
  700. NUTContext *nut = s->priv_data;
  701. ByteIOContext *bc = s->pb;
  702. int64_t pos, pts, back_ptr;
  703. av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n", stream_index, *pos_arg, pos_limit);
  704. pos= *pos_arg;
  705. do{
  706. pos= find_startcode(bc, SYNCPOINT_STARTCODE, pos)+1;
  707. if(pos < 1){
  708. assert(nut->next_startcode == 0);
  709. av_log(s, AV_LOG_ERROR, "read_timestamp failed.\n");
  710. return AV_NOPTS_VALUE;
  711. }
  712. }while(decode_syncpoint(nut, &pts, &back_ptr) < 0);
  713. *pos_arg = pos-1;
  714. assert(nut->last_syncpoint_pos == *pos_arg);
  715. av_log(s, AV_LOG_DEBUG, "return %"PRId64" %"PRId64"\n", pts,back_ptr );
  716. if (stream_index == -1) return pts;
  717. else if(stream_index == -2) return back_ptr;
  718. assert(0);
  719. }
  720. static int read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags){
  721. NUTContext *nut = s->priv_data;
  722. AVStream *st= s->streams[stream_index];
  723. syncpoint_t dummy={.ts= pts*av_q2d(st->time_base)*AV_TIME_BASE};
  724. syncpoint_t nopts_sp= {.ts= AV_NOPTS_VALUE, .back_ptr= AV_NOPTS_VALUE};
  725. syncpoint_t *sp, *next_node[2]= {&nopts_sp, &nopts_sp};
  726. int64_t pos, pos2, ts;
  727. int i;
  728. if(st->index_entries){
  729. int index= av_index_search_timestamp(st, pts, flags);
  730. if(index<0)
  731. return -1;
  732. pos2= st->index_entries[index].pos;
  733. ts = st->index_entries[index].timestamp;
  734. }else{
  735. av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pts_cmp, next_node);
  736. av_log(s, AV_LOG_DEBUG, "%"PRIu64"-%"PRIu64" %"PRId64"-%"PRId64"\n", next_node[0]->pos, next_node[1]->pos,
  737. next_node[0]->ts , next_node[1]->ts);
  738. pos= av_gen_search(s, -1, dummy.ts, next_node[0]->pos, next_node[1]->pos, next_node[1]->pos,
  739. next_node[0]->ts , next_node[1]->ts, AVSEEK_FLAG_BACKWARD, &ts, nut_read_timestamp);
  740. if(!(flags & AVSEEK_FLAG_BACKWARD)){
  741. dummy.pos= pos+16;
  742. next_node[1]= &nopts_sp;
  743. av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp, next_node);
  744. pos2= av_gen_search(s, -2, dummy.pos, next_node[0]->pos , next_node[1]->pos, next_node[1]->pos,
  745. next_node[0]->back_ptr, next_node[1]->back_ptr, flags, &ts, nut_read_timestamp);
  746. if(pos2>=0)
  747. pos= pos2;
  748. //FIXME dir but i think it does not matter
  749. }
  750. dummy.pos= pos;
  751. sp= av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp, NULL);
  752. assert(sp);
  753. pos2= sp->back_ptr - 15;
  754. }
  755. av_log(NULL, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos2);
  756. pos= find_startcode(s->pb, SYNCPOINT_STARTCODE, pos2);
  757. url_fseek(s->pb, pos, SEEK_SET);
  758. av_log(NULL, AV_LOG_DEBUG, "SP: %"PRId64"\n", pos);
  759. if(pos2 > pos || pos2 + 15 < pos){
  760. av_log(NULL, AV_LOG_ERROR, "no syncpoint at backptr pos\n");
  761. }
  762. for(i=0; i<s->nb_streams; i++)
  763. nut->stream[i].skip_until_key_frame=1;
  764. return 0;
  765. }
  766. static int nut_read_close(AVFormatContext *s)
  767. {
  768. NUTContext *nut = s->priv_data;
  769. av_freep(&nut->time_base);
  770. av_freep(&nut->stream);
  771. return 0;
  772. }
  773. #ifdef CONFIG_NUT_DEMUXER
  774. AVInputFormat nut_demuxer = {
  775. "nut",
  776. "nut format",
  777. sizeof(NUTContext),
  778. nut_probe,
  779. nut_read_header,
  780. nut_read_packet,
  781. nut_read_close,
  782. read_seek,
  783. .extensions = "nut",
  784. };
  785. #endif