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.

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