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.

914 lines
29KB

  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 "libavutil/avstring.h"
  23. #include "libavutil/tree.h"
  24. #include "nut.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 currently are
  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. AVChapter *chapter= NULL;
  344. end= get_packetheader(nut, bc, 1, INFO_STARTCODE);
  345. end += url_ftell(bc);
  346. GET_V(stream_id_plus1, tmp <= s->nb_streams)
  347. chapter_id = get_s(bc);
  348. chapter_start= ff_get_v(bc);
  349. chapter_len = ff_get_v(bc);
  350. count = ff_get_v(bc);
  351. if(chapter_id && !stream_id_plus1){
  352. int64_t start= chapter_start / nut->time_base_count;
  353. chapter= ff_new_chapter(s, chapter_id,
  354. nut->time_base[chapter_start % nut->time_base_count],
  355. start, start + chapter_len, NULL);
  356. }
  357. for(i=0; i<count; i++){
  358. get_str(bc, name, sizeof(name));
  359. value= get_s(bc);
  360. if(value == -1){
  361. type= "UTF-8";
  362. get_str(bc, str_value, sizeof(str_value));
  363. }else if(value == -2){
  364. get_str(bc, type_str, sizeof(type_str));
  365. type= type_str;
  366. get_str(bc, str_value, sizeof(str_value));
  367. }else if(value == -3){
  368. type= "s";
  369. value= get_s(bc);
  370. }else if(value == -4){
  371. type= "t";
  372. value= ff_get_v(bc);
  373. }else if(value < -4){
  374. type= "r";
  375. get_s(bc);
  376. }else{
  377. type= "v";
  378. }
  379. if (stream_id_plus1 > s->nb_streams) {
  380. av_log(s, AV_LOG_ERROR, "invalid stream id for info packet\n");
  381. continue;
  382. }
  383. if(chapter_id==0 && !strcmp(type, "UTF-8")){
  384. if (!strcmp(name, "Author"))
  385. av_strlcpy(s->author , str_value, sizeof(s->author));
  386. else if(!strcmp(name, "Title"))
  387. av_strlcpy(s->title , str_value, sizeof(s->title));
  388. else if(!strcmp(name, "Copyright"))
  389. av_strlcpy(s->copyright, str_value, sizeof(s->copyright));
  390. else if(!strcmp(name, "Description"))
  391. av_strlcpy(s->comment , str_value, sizeof(s->comment));
  392. else if(!strcmp(name, "Disposition"))
  393. set_disposition_bits(s, str_value, stream_id_plus1 - 1);
  394. }
  395. if(chapter && !strcmp(type, "UTF-8")){
  396. if(!strcmp(name, "Title"))
  397. chapter->title= av_strdup(str_value);
  398. }
  399. }
  400. if(skip_reserved(bc, end) || get_checksum(bc)){
  401. av_log(s, AV_LOG_ERROR, "info header checksum mismatch\n");
  402. return -1;
  403. }
  404. return 0;
  405. }
  406. static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr){
  407. AVFormatContext *s= nut->avf;
  408. ByteIOContext *bc = s->pb;
  409. int64_t end, tmp;
  410. nut->last_syncpoint_pos= url_ftell(bc)-8;
  411. end= get_packetheader(nut, bc, 1, SYNCPOINT_STARTCODE);
  412. end += url_ftell(bc);
  413. tmp= ff_get_v(bc);
  414. *back_ptr= nut->last_syncpoint_pos - 16*ff_get_v(bc);
  415. if(*back_ptr < 0)
  416. return -1;
  417. ff_nut_reset_ts(nut, nut->time_base[tmp % nut->time_base_count], tmp / nut->time_base_count);
  418. if(skip_reserved(bc, end) || get_checksum(bc)){
  419. av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n");
  420. return -1;
  421. }
  422. *ts= tmp / s->nb_streams * av_q2d(nut->time_base[tmp % s->nb_streams])*AV_TIME_BASE;
  423. ff_nut_add_sp(nut, nut->last_syncpoint_pos, *back_ptr, *ts);
  424. return 0;
  425. }
  426. static int find_and_decode_index(NUTContext *nut){
  427. AVFormatContext *s= nut->avf;
  428. ByteIOContext *bc = s->pb;
  429. uint64_t tmp, end;
  430. int i, j, syncpoint_count;
  431. int64_t filesize= url_fsize(bc);
  432. int64_t *syncpoints;
  433. int8_t *has_keyframe;
  434. url_fseek(bc, filesize-12, SEEK_SET);
  435. url_fseek(bc, filesize-get_be64(bc), SEEK_SET);
  436. if(get_be64(bc) != INDEX_STARTCODE){
  437. av_log(s, AV_LOG_ERROR, "no index at the end\n");
  438. return -1;
  439. }
  440. end= get_packetheader(nut, bc, 1, INDEX_STARTCODE);
  441. end += url_ftell(bc);
  442. ff_get_v(bc); //max_pts
  443. GET_V(syncpoint_count, tmp < INT_MAX/8 && tmp > 0)
  444. syncpoints= av_malloc(sizeof(int64_t)*syncpoint_count);
  445. has_keyframe= av_malloc(sizeof(int8_t)*(syncpoint_count+1));
  446. for(i=0; i<syncpoint_count; i++){
  447. GET_V(syncpoints[i], tmp>0)
  448. if(i)
  449. syncpoints[i] += syncpoints[i-1];
  450. }
  451. for(i=0; i<s->nb_streams; i++){
  452. int64_t last_pts= -1;
  453. for(j=0; j<syncpoint_count;){
  454. uint64_t x= ff_get_v(bc);
  455. int type= x&1;
  456. int n= j;
  457. x>>=1;
  458. if(type){
  459. int flag= x&1;
  460. x>>=1;
  461. if(n+x >= syncpoint_count + 1){
  462. av_log(s, AV_LOG_ERROR, "index overflow A\n");
  463. return -1;
  464. }
  465. while(x--)
  466. has_keyframe[n++]= flag;
  467. has_keyframe[n++]= !flag;
  468. }else{
  469. while(x != 1){
  470. if(n>=syncpoint_count + 1){
  471. av_log(s, AV_LOG_ERROR, "index overflow B\n");
  472. return -1;
  473. }
  474. has_keyframe[n++]= x&1;
  475. x>>=1;
  476. }
  477. }
  478. if(has_keyframe[0]){
  479. av_log(s, AV_LOG_ERROR, "keyframe before first syncpoint in index\n");
  480. return -1;
  481. }
  482. assert(n<=syncpoint_count+1);
  483. for(; j<n && j<syncpoint_count; j++){
  484. if(has_keyframe[j]){
  485. uint64_t B, A= ff_get_v(bc);
  486. if(!A){
  487. A= ff_get_v(bc);
  488. B= ff_get_v(bc);
  489. //eor_pts[j][i] = last_pts + A + B
  490. }else
  491. B= 0;
  492. av_add_index_entry(
  493. s->streams[i],
  494. 16*syncpoints[j-1],
  495. last_pts + A,
  496. 0,
  497. 0,
  498. AVINDEX_KEYFRAME);
  499. last_pts += A + B;
  500. }
  501. }
  502. }
  503. }
  504. if(skip_reserved(bc, end) || get_checksum(bc)){
  505. av_log(s, AV_LOG_ERROR, "index checksum mismatch\n");
  506. return -1;
  507. }
  508. return 0;
  509. }
  510. static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
  511. {
  512. NUTContext *nut = s->priv_data;
  513. ByteIOContext *bc = s->pb;
  514. int64_t pos;
  515. int initialized_stream_count;
  516. nut->avf= s;
  517. /* main header */
  518. pos=0;
  519. do{
  520. pos= find_startcode(bc, MAIN_STARTCODE, pos)+1;
  521. if (pos<0+1){
  522. av_log(s, AV_LOG_ERROR, "No main startcode found.\n");
  523. return -1;
  524. }
  525. }while(decode_main_header(nut) < 0);
  526. /* stream headers */
  527. pos=0;
  528. for(initialized_stream_count=0; initialized_stream_count < s->nb_streams;){
  529. pos= find_startcode(bc, STREAM_STARTCODE, pos)+1;
  530. if (pos<0+1){
  531. av_log(s, AV_LOG_ERROR, "Not all stream headers found.\n");
  532. return -1;
  533. }
  534. if(decode_stream_header(nut) >= 0)
  535. initialized_stream_count++;
  536. }
  537. /* info headers */
  538. pos=0;
  539. for(;;){
  540. uint64_t startcode= find_any_startcode(bc, pos);
  541. pos= url_ftell(bc);
  542. if(startcode==0){
  543. av_log(s, AV_LOG_ERROR, "EOF before video frames\n");
  544. return -1;
  545. }else if(startcode == SYNCPOINT_STARTCODE){
  546. nut->next_startcode= startcode;
  547. break;
  548. }else if(startcode != INFO_STARTCODE){
  549. continue;
  550. }
  551. decode_info_header(nut);
  552. }
  553. s->data_offset= pos-8;
  554. if(!url_is_streamed(bc)){
  555. int64_t orig_pos= url_ftell(bc);
  556. find_and_decode_index(nut);
  557. url_fseek(bc, orig_pos, SEEK_SET);
  558. }
  559. assert(nut->next_startcode == SYNCPOINT_STARTCODE);
  560. return 0;
  561. }
  562. static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id, uint8_t *header_idx, int frame_code){
  563. AVFormatContext *s= nut->avf;
  564. ByteIOContext *bc = s->pb;
  565. StreamContext *stc;
  566. int size, flags, size_mul, pts_delta, i, reserved_count;
  567. uint64_t tmp;
  568. if(url_ftell(bc) > nut->last_syncpoint_pos + nut->max_distance){
  569. 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);
  570. return -1;
  571. }
  572. flags = nut->frame_code[frame_code].flags;
  573. size_mul = nut->frame_code[frame_code].size_mul;
  574. size = nut->frame_code[frame_code].size_lsb;
  575. *stream_id = nut->frame_code[frame_code].stream_id;
  576. pts_delta = nut->frame_code[frame_code].pts_delta;
  577. reserved_count = nut->frame_code[frame_code].reserved_count;
  578. *header_idx = nut->frame_code[frame_code].header_idx;
  579. if(flags & FLAG_INVALID)
  580. return -1;
  581. if(flags & FLAG_CODED)
  582. flags ^= ff_get_v(bc);
  583. if(flags & FLAG_STREAM_ID){
  584. GET_V(*stream_id, tmp < s->nb_streams)
  585. }
  586. stc= &nut->stream[*stream_id];
  587. if(flags&FLAG_CODED_PTS){
  588. int coded_pts= ff_get_v(bc);
  589. //FIXME check last_pts validity?
  590. if(coded_pts < (1<<stc->msb_pts_shift)){
  591. *pts=ff_lsb2full(stc, coded_pts);
  592. }else
  593. *pts=coded_pts - (1<<stc->msb_pts_shift);
  594. }else
  595. *pts= stc->last_pts + pts_delta;
  596. if(flags&FLAG_SIZE_MSB){
  597. size += size_mul*ff_get_v(bc);
  598. }
  599. if(flags&FLAG_MATCH_TIME)
  600. get_s(bc);
  601. if(flags&FLAG_HEADER_IDX)
  602. *header_idx= ff_get_v(bc);
  603. if(flags&FLAG_RESERVED)
  604. reserved_count= ff_get_v(bc);
  605. for(i=0; i<reserved_count; i++)
  606. ff_get_v(bc);
  607. if(*header_idx >= (unsigned)nut->header_count){
  608. av_log(s, AV_LOG_ERROR, "header_idx invalid\n");
  609. return -1;
  610. }
  611. if(size > 4096)
  612. *header_idx=0;
  613. size -= nut->header_len[*header_idx];
  614. if(flags&FLAG_CHECKSUM){
  615. get_be32(bc); //FIXME check this
  616. }else if(size > 2*nut->max_distance || FFABS(stc->last_pts - *pts) > stc->max_pts_distance){
  617. av_log(s, AV_LOG_ERROR, "frame size > 2max_distance and no checksum\n");
  618. return -1;
  619. }
  620. stc->last_pts= *pts;
  621. stc->last_flags= flags;
  622. return size;
  623. }
  624. static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code){
  625. AVFormatContext *s= nut->avf;
  626. ByteIOContext *bc = s->pb;
  627. int size, stream_id, discard;
  628. int64_t pts, last_IP_pts;
  629. StreamContext *stc;
  630. uint8_t header_idx;
  631. size= decode_frame_header(nut, &pts, &stream_id, &header_idx, frame_code);
  632. if(size < 0)
  633. return -1;
  634. stc= &nut->stream[stream_id];
  635. if (stc->last_flags & FLAG_KEY)
  636. stc->skip_until_key_frame=0;
  637. discard= s->streams[ stream_id ]->discard;
  638. last_IP_pts= s->streams[ stream_id ]->last_IP_pts;
  639. if( (discard >= AVDISCARD_NONKEY && !(stc->last_flags & FLAG_KEY))
  640. ||(discard >= AVDISCARD_BIDIR && last_IP_pts != AV_NOPTS_VALUE && last_IP_pts > pts)
  641. || discard >= AVDISCARD_ALL
  642. || stc->skip_until_key_frame){
  643. url_fskip(bc, size);
  644. return 1;
  645. }
  646. av_new_packet(pkt, size + nut->header_len[header_idx]);
  647. memcpy(pkt->data, nut->header[header_idx], nut->header_len[header_idx]);
  648. pkt->pos= url_ftell(bc); //FIXME
  649. get_buffer(bc, pkt->data + nut->header_len[header_idx], size);
  650. pkt->stream_index = stream_id;
  651. if (stc->last_flags & FLAG_KEY)
  652. pkt->flags |= PKT_FLAG_KEY;
  653. pkt->pts = pts;
  654. return 0;
  655. }
  656. static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
  657. {
  658. NUTContext *nut = s->priv_data;
  659. ByteIOContext *bc = s->pb;
  660. int i, frame_code=0, ret, skip;
  661. int64_t ts, back_ptr;
  662. for(;;){
  663. int64_t pos= url_ftell(bc);
  664. uint64_t tmp= nut->next_startcode;
  665. nut->next_startcode=0;
  666. if(tmp){
  667. pos-=8;
  668. }else{
  669. frame_code = get_byte(bc);
  670. if(url_feof(bc))
  671. return -1;
  672. if(frame_code == 'N'){
  673. tmp= frame_code;
  674. for(i=1; i<8; i++)
  675. tmp = (tmp<<8) + get_byte(bc);
  676. }
  677. }
  678. switch(tmp){
  679. case MAIN_STARTCODE:
  680. case STREAM_STARTCODE:
  681. case INDEX_STARTCODE:
  682. skip= get_packetheader(nut, bc, 0, tmp);
  683. url_fseek(bc, skip, SEEK_CUR);
  684. break;
  685. case INFO_STARTCODE:
  686. if(decode_info_header(nut)<0)
  687. goto resync;
  688. break;
  689. case SYNCPOINT_STARTCODE:
  690. if(decode_syncpoint(nut, &ts, &back_ptr)<0)
  691. goto resync;
  692. frame_code = get_byte(bc);
  693. case 0:
  694. ret= decode_frame(nut, pkt, frame_code);
  695. if(ret==0)
  696. return 0;
  697. else if(ret==1) //ok but discard packet
  698. break;
  699. default:
  700. resync:
  701. av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", pos);
  702. tmp= find_any_startcode(bc, nut->last_syncpoint_pos+1);
  703. if(tmp==0)
  704. return -1;
  705. av_log(s, AV_LOG_DEBUG, "sync\n");
  706. nut->next_startcode= tmp;
  707. }
  708. }
  709. }
  710. static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index, int64_t *pos_arg, int64_t pos_limit){
  711. NUTContext *nut = s->priv_data;
  712. ByteIOContext *bc = s->pb;
  713. int64_t pos, pts, back_ptr;
  714. av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n", stream_index, *pos_arg, pos_limit);
  715. pos= *pos_arg;
  716. do{
  717. pos= find_startcode(bc, SYNCPOINT_STARTCODE, pos)+1;
  718. if(pos < 1){
  719. assert(nut->next_startcode == 0);
  720. av_log(s, AV_LOG_ERROR, "read_timestamp failed.\n");
  721. return AV_NOPTS_VALUE;
  722. }
  723. }while(decode_syncpoint(nut, &pts, &back_ptr) < 0);
  724. *pos_arg = pos-1;
  725. assert(nut->last_syncpoint_pos == *pos_arg);
  726. av_log(s, AV_LOG_DEBUG, "return %"PRId64" %"PRId64"\n", pts,back_ptr );
  727. if (stream_index == -1) return pts;
  728. else if(stream_index == -2) return back_ptr;
  729. assert(0);
  730. }
  731. static int read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags){
  732. NUTContext *nut = s->priv_data;
  733. AVStream *st= s->streams[stream_index];
  734. syncpoint_t dummy={.ts= pts*av_q2d(st->time_base)*AV_TIME_BASE};
  735. syncpoint_t nopts_sp= {.ts= AV_NOPTS_VALUE, .back_ptr= AV_NOPTS_VALUE};
  736. syncpoint_t *sp, *next_node[2]= {&nopts_sp, &nopts_sp};
  737. int64_t pos, pos2, ts;
  738. int i;
  739. if(st->index_entries){
  740. int index= av_index_search_timestamp(st, pts, flags);
  741. if(index<0)
  742. return -1;
  743. pos2= st->index_entries[index].pos;
  744. ts = st->index_entries[index].timestamp;
  745. }else{
  746. av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pts_cmp, next_node);
  747. av_log(s, AV_LOG_DEBUG, "%"PRIu64"-%"PRIu64" %"PRId64"-%"PRId64"\n", next_node[0]->pos, next_node[1]->pos,
  748. next_node[0]->ts , next_node[1]->ts);
  749. pos= av_gen_search(s, -1, dummy.ts, next_node[0]->pos, next_node[1]->pos, next_node[1]->pos,
  750. next_node[0]->ts , next_node[1]->ts, AVSEEK_FLAG_BACKWARD, &ts, nut_read_timestamp);
  751. if(!(flags & AVSEEK_FLAG_BACKWARD)){
  752. dummy.pos= pos+16;
  753. next_node[1]= &nopts_sp;
  754. av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp, next_node);
  755. pos2= av_gen_search(s, -2, dummy.pos, next_node[0]->pos , next_node[1]->pos, next_node[1]->pos,
  756. next_node[0]->back_ptr, next_node[1]->back_ptr, flags, &ts, nut_read_timestamp);
  757. if(pos2>=0)
  758. pos= pos2;
  759. //FIXME dir but I think it does not matter
  760. }
  761. dummy.pos= pos;
  762. sp= av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp, NULL);
  763. assert(sp);
  764. pos2= sp->back_ptr - 15;
  765. }
  766. av_log(NULL, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos2);
  767. pos= find_startcode(s->pb, SYNCPOINT_STARTCODE, pos2);
  768. url_fseek(s->pb, pos, SEEK_SET);
  769. av_log(NULL, AV_LOG_DEBUG, "SP: %"PRId64"\n", pos);
  770. if(pos2 > pos || pos2 + 15 < pos){
  771. av_log(NULL, AV_LOG_ERROR, "no syncpoint at backptr pos\n");
  772. }
  773. for(i=0; i<s->nb_streams; i++)
  774. nut->stream[i].skip_until_key_frame=1;
  775. return 0;
  776. }
  777. static int nut_read_close(AVFormatContext *s)
  778. {
  779. NUTContext *nut = s->priv_data;
  780. av_freep(&nut->time_base);
  781. av_freep(&nut->stream);
  782. return 0;
  783. }
  784. #ifdef CONFIG_NUT_DEMUXER
  785. AVInputFormat nut_demuxer = {
  786. "nut",
  787. "nut format",
  788. sizeof(NUTContext),
  789. nut_probe,
  790. nut_read_header,
  791. nut_read_packet,
  792. nut_read_close,
  793. read_seek,
  794. .extensions = "nut",
  795. };
  796. #endif