nutenc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. /*
  2. * nut muxer
  3. * Copyright (c) 2004-2007 Michael Niedermayer
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "nut.h"
  22. #include "tree.h"
  23. #include "mpegaudiodata.h"
  24. static int find_expected_header(AVCodecContext *c, int size, int key_frame, uint8_t out[64]){
  25. int sample_rate= c->sample_rate;
  26. if(size>4096)
  27. return 0;
  28. AV_WB24(out, 1);
  29. if(c->codec_id == CODEC_ID_MPEG4){
  30. if(key_frame){
  31. return 3;
  32. }else{
  33. out[3]= 0xB6;
  34. return 4;
  35. }
  36. }else if(c->codec_id == CODEC_ID_MPEG1VIDEO || c->codec_id == CODEC_ID_MPEG2VIDEO){
  37. return 3;
  38. }else if(c->codec_id == CODEC_ID_H264){
  39. return 3;
  40. }else if(c->codec_id == CODEC_ID_MP3 || c->codec_id == CODEC_ID_MP2){
  41. int lsf, mpeg25, sample_rate_index, bitrate_index, frame_size;
  42. int layer= c->codec_id == CODEC_ID_MP3 ? 3 : 2;
  43. unsigned int header= 0xFFF00000;
  44. lsf = sample_rate < (24000+32000)/2;
  45. mpeg25 = sample_rate < (12000+16000)/2;
  46. sample_rate <<= lsf + mpeg25;
  47. if (sample_rate < (32000 + 44100)/2) sample_rate_index=2;
  48. else if(sample_rate < (44100 + 48000)/2) sample_rate_index=0;
  49. else sample_rate_index=1;
  50. sample_rate= ff_mpa_freq_tab[sample_rate_index] >> (lsf + mpeg25);
  51. for(bitrate_index=2; bitrate_index<30; bitrate_index++){
  52. frame_size = ff_mpa_bitrate_tab[lsf][layer-1][bitrate_index>>1];
  53. frame_size = (frame_size * 144000) / (sample_rate << lsf) + (bitrate_index&1);
  54. if(frame_size == size)
  55. break;
  56. }
  57. header |= (!lsf)<<19;
  58. header |= (4-layer)<<17;
  59. header |= 1<<16; //no crc
  60. AV_WB32(out, header);
  61. if(size <= 0)
  62. return 2; //we guess there is no crc, if there is one the user clearly does not care about overhead
  63. if(bitrate_index == 30)
  64. return -1; //something is wrong ...
  65. header |= (bitrate_index>>1)<<12;
  66. header |= sample_rate_index<<10;
  67. header |= (bitrate_index&1)<<9;
  68. return 2; //FIXME actually put the needed ones in build_elision_headers()
  69. return 3; //we guess that the private bit is not set
  70. //FIXME the above assumptions should be checked, if these turn out false too often something should be done
  71. }
  72. return 0;
  73. }
  74. static int find_header_idx(AVFormatContext *s, AVCodecContext *c, int size, int frame_type){
  75. NUTContext *nut = s->priv_data;
  76. uint8_t out[64];
  77. int i;
  78. int len= find_expected_header(c, size, frame_type, out);
  79. //av_log(NULL, AV_LOG_ERROR, "expected_h len=%d size=%d codec_id=%d\n", len, size, c->codec_id);
  80. for(i=1; i<nut->header_count; i++){
  81. if( len == nut->header_len[i]
  82. && !memcmp(out, nut->header[i], len)){
  83. // av_log(NULL, AV_LOG_ERROR, "found %d\n", i);
  84. return i;
  85. }
  86. }
  87. // av_log(NULL, AV_LOG_ERROR, "nothing found\n");
  88. return 0;
  89. }
  90. static void build_elision_headers(AVFormatContext *s){
  91. NUTContext *nut = s->priv_data;
  92. int i;
  93. //FIXME this is lame
  94. //FIXME write a 2pass mode to find the maximal headers
  95. const static uint8_t headers[][5]={
  96. {3, 0x00, 0x00, 0x01},
  97. {4, 0x00, 0x00, 0x01, 0xB6},
  98. {2, 0xFF, 0xFA}, //mp3+crc
  99. {2, 0xFF, 0xFB}, //mp3
  100. {2, 0xFF, 0xFC}, //mp2+crc
  101. {2, 0xFF, 0xFD}, //mp2
  102. };
  103. nut->header_count= 7;
  104. for(i=1; i<nut->header_count; i++){
  105. nut->header_len[i]= headers[i-1][0];
  106. nut->header [i]= &headers[i-1][1];
  107. }
  108. }
  109. static void build_frame_code(AVFormatContext *s){
  110. NUTContext *nut = s->priv_data;
  111. int key_frame, index, pred, stream_id;
  112. int start=1;
  113. int end= 254;
  114. int keyframe_0_esc= s->nb_streams > 2;
  115. int pred_table[10];
  116. FrameCode *ft;
  117. ft= &nut->frame_code[start];
  118. ft->flags= FLAG_CODED;
  119. ft->size_mul=1;
  120. ft->pts_delta=1;
  121. start++;
  122. if(keyframe_0_esc){
  123. /* keyframe = 0 escape */
  124. FrameCode *ft= &nut->frame_code[start];
  125. ft->flags= FLAG_STREAM_ID | FLAG_SIZE_MSB | FLAG_CODED_PTS;
  126. ft->size_mul=1;
  127. start++;
  128. }
  129. for(stream_id= 0; stream_id<s->nb_streams; stream_id++){
  130. int start2= start + (end-start)*stream_id / s->nb_streams;
  131. int end2 = start + (end-start)*(stream_id+1) / s->nb_streams;
  132. AVCodecContext *codec = s->streams[stream_id]->codec;
  133. int is_audio= codec->codec_type == CODEC_TYPE_AUDIO;
  134. int intra_only= /*codec->intra_only || */is_audio;
  135. int pred_count;
  136. for(key_frame=0; key_frame<2; key_frame++){
  137. if(intra_only && keyframe_0_esc && key_frame==0)
  138. continue;
  139. {
  140. FrameCode *ft= &nut->frame_code[start2];
  141. ft->flags= FLAG_KEY*key_frame;
  142. ft->flags|= FLAG_SIZE_MSB | FLAG_CODED_PTS;
  143. ft->stream_id= stream_id;
  144. ft->size_mul=1;
  145. if(is_audio)
  146. ft->header_idx= find_header_idx(s, codec, -1, key_frame);
  147. start2++;
  148. }
  149. }
  150. key_frame= intra_only;
  151. #if 1
  152. if(is_audio){
  153. int frame_bytes= codec->frame_size*(int64_t)codec->bit_rate / (8*codec->sample_rate);
  154. int pts;
  155. for(pts=0; pts<2; pts++){
  156. for(pred=0; pred<2; pred++){
  157. FrameCode *ft= &nut->frame_code[start2];
  158. ft->flags= FLAG_KEY*key_frame;
  159. ft->stream_id= stream_id;
  160. ft->size_mul=frame_bytes + 2;
  161. ft->size_lsb=frame_bytes + pred;
  162. ft->pts_delta=pts;
  163. ft->header_idx= find_header_idx(s, codec, frame_bytes + pred, key_frame);
  164. start2++;
  165. }
  166. }
  167. }else{
  168. FrameCode *ft= &nut->frame_code[start2];
  169. ft->flags= FLAG_KEY | FLAG_SIZE_MSB;
  170. ft->stream_id= stream_id;
  171. ft->size_mul=1;
  172. ft->pts_delta=1;
  173. start2++;
  174. }
  175. #endif
  176. if(codec->has_b_frames){
  177. pred_count=5;
  178. pred_table[0]=-2;
  179. pred_table[1]=-1;
  180. pred_table[2]=1;
  181. pred_table[3]=3;
  182. pred_table[4]=4;
  183. }else if(codec->codec_id == CODEC_ID_VORBIS){
  184. pred_count=3;
  185. pred_table[0]=2;
  186. pred_table[1]=9;
  187. pred_table[2]=16;
  188. }else{
  189. pred_count=1;
  190. pred_table[0]=1;
  191. }
  192. for(pred=0; pred<pred_count; pred++){
  193. int start3= start2 + (end2-start2)*pred / pred_count;
  194. int end3 = start2 + (end2-start2)*(pred+1) / pred_count;
  195. for(index=start3; index<end3; index++){
  196. FrameCode *ft= &nut->frame_code[index];
  197. ft->flags= FLAG_KEY*key_frame;
  198. ft->flags|= FLAG_SIZE_MSB;
  199. ft->stream_id= stream_id;
  200. //FIXME use single byte size and pred from last
  201. ft->size_mul= end3-start3;
  202. ft->size_lsb= index - start3;
  203. ft->pts_delta= pred_table[pred];
  204. if(is_audio)
  205. ft->header_idx= find_header_idx(s, codec, -1, key_frame);
  206. }
  207. }
  208. }
  209. memmove(&nut->frame_code['N'+1], &nut->frame_code['N'], sizeof(FrameCode)*(255-'N'));
  210. nut->frame_code[ 0].flags=
  211. nut->frame_code[255].flags=
  212. nut->frame_code['N'].flags= FLAG_INVALID;
  213. }
  214. /**
  215. * Gets the length in bytes which is needed to store val as v.
  216. */
  217. static int get_length(uint64_t val){
  218. int i=1;
  219. while(val>>=7)
  220. i++;
  221. return i;
  222. }
  223. static void put_v(ByteIOContext *bc, uint64_t val){
  224. int i= get_length(val);
  225. while(--i>0)
  226. put_byte(bc, 128 | (val>>(7*i)));
  227. put_byte(bc, val&127);
  228. }
  229. static void put_t(NUTContext *nut, StreamContext *nus, ByteIOContext *bc, uint64_t val){
  230. val *= nut->time_base_count;
  231. val += nus->time_base - nut->time_base;
  232. put_v(bc, val);
  233. }
  234. /**
  235. * Stores a string as vb.
  236. */
  237. static void put_str(ByteIOContext *bc, const char *string){
  238. int len= strlen(string);
  239. put_v(bc, len);
  240. put_buffer(bc, string, len);
  241. }
  242. static void put_s(ByteIOContext *bc, int64_t val){
  243. put_v(bc, 2*FFABS(val) - (val>0));
  244. }
  245. #ifdef TRACE
  246. static inline void put_v_trace(ByteIOContext *bc, uint64_t v, char *file, char *func, int line){
  247. av_log(NULL, AV_LOG_DEBUG, "put_v %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
  248. put_v(bc, v);
  249. }
  250. static inline void put_s_trace(ByteIOContext *bc, int64_t v, char *file, char *func, int line){
  251. av_log(NULL, AV_LOG_DEBUG, "put_s %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
  252. put_s(bc, v);
  253. }
  254. #define put_v(bc, v) put_v_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  255. #define put_s(bc, v) put_s_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  256. #endif
  257. //FIXME remove calculate_checksum
  258. static void put_packet(NUTContext *nut, ByteIOContext *bc, ByteIOContext *dyn_bc, int calculate_checksum, uint64_t startcode){
  259. uint8_t *dyn_buf=NULL;
  260. int dyn_size= url_close_dyn_buf(dyn_bc, &dyn_buf);
  261. int forw_ptr= dyn_size + 4*calculate_checksum;
  262. if(forw_ptr > 4096)
  263. init_checksum(bc, ff_crc04C11DB7_update, 0);
  264. put_be64(bc, startcode);
  265. put_v(bc, forw_ptr);
  266. if(forw_ptr > 4096)
  267. put_le32(bc, get_checksum(bc));
  268. if(calculate_checksum)
  269. init_checksum(bc, ff_crc04C11DB7_update, 0);
  270. put_buffer(bc, dyn_buf, dyn_size);
  271. if(calculate_checksum)
  272. put_le32(bc, get_checksum(bc));
  273. av_free(dyn_buf);
  274. }
  275. static void write_mainheader(NUTContext *nut, ByteIOContext *bc){
  276. int i, j, tmp_pts, tmp_flags, tmp_stream, tmp_mul, tmp_size, tmp_fields, tmp_head_idx;
  277. int64_t tmp_match;
  278. put_v(bc, 3); /* version */
  279. put_v(bc, nut->avf->nb_streams);
  280. put_v(bc, nut->max_distance);
  281. put_v(bc, nut->time_base_count);
  282. for(i=0; i<nut->time_base_count; i++){
  283. put_v(bc, nut->time_base[i].num);
  284. put_v(bc, nut->time_base[i].den);
  285. }
  286. tmp_pts=0;
  287. tmp_mul=1;
  288. tmp_stream=0;
  289. tmp_match= 1-(1LL<<62);
  290. tmp_head_idx= 0;
  291. for(i=0; i<256;){
  292. tmp_fields=0;
  293. tmp_size=0;
  294. // tmp_res=0;
  295. if(tmp_pts != nut->frame_code[i].pts_delta) tmp_fields=1;
  296. if(tmp_mul != nut->frame_code[i].size_mul ) tmp_fields=2;
  297. if(tmp_stream != nut->frame_code[i].stream_id) tmp_fields=3;
  298. if(tmp_size != nut->frame_code[i].size_lsb ) tmp_fields=4;
  299. // if(tmp_res != nut->frame_code[i].res ) tmp_fields=5;
  300. if(tmp_head_idx!=nut->frame_code[i].header_idx)tmp_fields=8;
  301. tmp_pts = nut->frame_code[i].pts_delta;
  302. tmp_flags = nut->frame_code[i].flags;
  303. tmp_stream= nut->frame_code[i].stream_id;
  304. tmp_mul = nut->frame_code[i].size_mul;
  305. tmp_size = nut->frame_code[i].size_lsb;
  306. // tmp_res = nut->frame_code[i].res;
  307. tmp_head_idx= nut->frame_code[i].header_idx;
  308. for(j=0; i<256; j++,i++){
  309. if(i == 'N'){
  310. j--;
  311. continue;
  312. }
  313. if(nut->frame_code[i].pts_delta != tmp_pts ) break;
  314. if(nut->frame_code[i].flags != tmp_flags ) break;
  315. if(nut->frame_code[i].stream_id != tmp_stream) break;
  316. if(nut->frame_code[i].size_mul != tmp_mul ) break;
  317. if(nut->frame_code[i].size_lsb != tmp_size+j) break;
  318. // if(nut->frame_code[i].res != tmp_res ) break;
  319. if(nut->frame_code[i].header_idx!= tmp_head_idx) break;
  320. }
  321. if(j != tmp_mul - tmp_size) tmp_fields=6;
  322. put_v(bc, tmp_flags);
  323. put_v(bc, tmp_fields);
  324. if(tmp_fields>0) put_s(bc, tmp_pts);
  325. if(tmp_fields>1) put_v(bc, tmp_mul);
  326. if(tmp_fields>2) put_v(bc, tmp_stream);
  327. if(tmp_fields>3) put_v(bc, tmp_size);
  328. if(tmp_fields>4) put_v(bc, 0 /*tmp_res*/);
  329. if(tmp_fields>5) put_v(bc, j);
  330. if(tmp_fields>6) put_v(bc, tmp_match);
  331. if(tmp_fields>7) put_v(bc, tmp_head_idx);
  332. }
  333. put_v(bc, nut->header_count-1);
  334. for(i=1; i<nut->header_count; i++){
  335. put_v(bc, nut->header_len[i]);
  336. put_buffer(bc, nut->header[i], nut->header_len[i]);
  337. }
  338. }
  339. static int write_streamheader(NUTContext *nut, ByteIOContext *bc, AVCodecContext *codec, int i){
  340. put_v(bc, i);
  341. switch(codec->codec_type){
  342. case CODEC_TYPE_VIDEO: put_v(bc, 0); break;
  343. case CODEC_TYPE_AUDIO: put_v(bc, 1); break;
  344. case CODEC_TYPE_SUBTITLE: put_v(bc, 2); break;
  345. default : put_v(bc, 3); break;
  346. }
  347. put_v(bc, 4);
  348. if (codec->codec_tag){
  349. put_le32(bc, codec->codec_tag);
  350. }else
  351. return -1;
  352. put_v(bc, nut->stream[i].time_base - nut->time_base);
  353. put_v(bc, nut->stream[i].msb_pts_shift);
  354. put_v(bc, nut->stream[i].max_pts_distance);
  355. put_v(bc, codec->has_b_frames);
  356. put_byte(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
  357. put_v(bc, codec->extradata_size);
  358. put_buffer(bc, codec->extradata, codec->extradata_size);
  359. switch(codec->codec_type){
  360. case CODEC_TYPE_AUDIO:
  361. put_v(bc, codec->sample_rate);
  362. put_v(bc, 1);
  363. put_v(bc, codec->channels);
  364. break;
  365. case CODEC_TYPE_VIDEO:
  366. put_v(bc, codec->width);
  367. put_v(bc, codec->height);
  368. if(codec->sample_aspect_ratio.num<=0 || codec->sample_aspect_ratio.den<=0){
  369. put_v(bc, 0);
  370. put_v(bc, 0);
  371. }else{
  372. put_v(bc, codec->sample_aspect_ratio.num);
  373. put_v(bc, codec->sample_aspect_ratio.den);
  374. }
  375. put_v(bc, 0); /* csp type -- unknown */
  376. break;
  377. default:
  378. break;
  379. }
  380. return 0;
  381. }
  382. static int add_info(ByteIOContext *bc, const char *type, const char *value){
  383. put_str(bc, type);
  384. put_s(bc, -1);
  385. put_str(bc, value);
  386. return 1;
  387. }
  388. static int write_globalinfo(NUTContext *nut, ByteIOContext *bc){
  389. AVFormatContext *s= nut->avf;
  390. ByteIOContext *dyn_bc;
  391. uint8_t *dyn_buf=NULL;
  392. int count=0, dyn_size;
  393. int ret = url_open_dyn_buf(&dyn_bc);
  394. if(ret < 0)
  395. return ret;
  396. if(s->title [0]) count+= add_info(dyn_bc, "Title" , s->title);
  397. if(s->author [0]) count+= add_info(dyn_bc, "Author" , s->author);
  398. if(s->copyright[0]) count+= add_info(dyn_bc, "Copyright", s->copyright);
  399. if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
  400. count+= add_info(dyn_bc, "Encoder" , LIBAVFORMAT_IDENT);
  401. put_v(bc, 0); //stream_if_plus1
  402. put_v(bc, 0); //chapter_id
  403. put_v(bc, 0); //timestamp_start
  404. put_v(bc, 0); //length
  405. put_v(bc, count);
  406. dyn_size= url_close_dyn_buf(dyn_bc, &dyn_buf);
  407. put_buffer(bc, dyn_buf, dyn_size);
  408. av_free(dyn_buf);
  409. return 0;
  410. }
  411. static int write_streaminfo(NUTContext *nut, ByteIOContext *bc, int stream_id){
  412. AVFormatContext *s= nut->avf;
  413. AVStream* st = s->streams[stream_id];
  414. ByteIOContext *dyn_bc;
  415. uint8_t *dyn_buf=NULL;
  416. int count=0, dyn_size, i;
  417. int ret = url_open_dyn_buf(&dyn_bc);
  418. if(ret < 0)
  419. return ret;
  420. for (i=0; ff_nut_dispositions[i].flag; ++i) {
  421. if (st->disposition & ff_nut_dispositions[i].flag)
  422. count += add_info(dyn_bc, "Disposition", ff_nut_dispositions[i].str);
  423. }
  424. dyn_size = url_close_dyn_buf(dyn_bc, &dyn_buf);
  425. if (count) {
  426. put_v(bc, stream_id + 1); //stream_id_plus1
  427. put_v(bc, 0); //chapter_id
  428. put_v(bc, 0); //timestamp_start
  429. put_v(bc, 0); //length
  430. put_v(bc, count);
  431. put_buffer(bc, dyn_buf, dyn_size);
  432. }
  433. av_free(dyn_buf);
  434. return count;
  435. }
  436. static int write_headers(NUTContext *nut, ByteIOContext *bc){
  437. ByteIOContext *dyn_bc;
  438. int i, ret;
  439. ret = url_open_dyn_buf(&dyn_bc);
  440. if(ret < 0)
  441. return ret;
  442. write_mainheader(nut, dyn_bc);
  443. put_packet(nut, bc, dyn_bc, 1, MAIN_STARTCODE);
  444. for (i=0; i < nut->avf->nb_streams; i++){
  445. AVCodecContext *codec = nut->avf->streams[i]->codec;
  446. ret = url_open_dyn_buf(&dyn_bc);
  447. if(ret < 0)
  448. return ret;
  449. write_streamheader(nut, dyn_bc, codec, i);
  450. put_packet(nut, bc, dyn_bc, 1, STREAM_STARTCODE);
  451. }
  452. ret = url_open_dyn_buf(&dyn_bc);
  453. if(ret < 0)
  454. return ret;
  455. write_globalinfo(nut, dyn_bc);
  456. put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE);
  457. for (i = 0; i < nut->avf->nb_streams; i++) {
  458. ret = url_open_dyn_buf(&dyn_bc);
  459. if(ret < 0)
  460. return ret;
  461. ret = write_streaminfo(nut, dyn_bc, i);
  462. if (ret < 0)
  463. return ret;
  464. if (ret > 0)
  465. put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE);
  466. else {
  467. uint8_t* buf;
  468. url_close_dyn_buf(dyn_bc, &buf);
  469. av_free(buf);
  470. }
  471. }
  472. nut->last_syncpoint_pos= INT_MIN;
  473. nut->header_count++;
  474. return 0;
  475. }
  476. static int write_header(AVFormatContext *s){
  477. NUTContext *nut = s->priv_data;
  478. ByteIOContext *bc = s->pb;
  479. int i, j;
  480. nut->avf= s;
  481. nut->stream = av_mallocz(sizeof(StreamContext)*s->nb_streams);
  482. nut->time_base= av_mallocz(sizeof(AVRational )*s->nb_streams);
  483. for(i=0; i<s->nb_streams; i++){
  484. AVStream *st= s->streams[i];
  485. int ssize;
  486. AVRational time_base;
  487. ff_parse_specific_params(st->codec, &time_base.den, &ssize, &time_base.num);
  488. av_set_pts_info(st, 64, time_base.num, time_base.den);
  489. for(j=0; j<nut->time_base_count; j++){
  490. if(!memcmp(&time_base, &nut->time_base[j], sizeof(AVRational))){
  491. break;
  492. }
  493. }
  494. nut->time_base[j]= time_base;
  495. nut->stream[i].time_base= &nut->time_base[j];
  496. if(j==nut->time_base_count)
  497. nut->time_base_count++;
  498. if(av_q2d(time_base) >= 0.001)
  499. nut->stream[i].msb_pts_shift = 7;
  500. else
  501. nut->stream[i].msb_pts_shift = 14;
  502. nut->stream[i].max_pts_distance= FFMAX(1/av_q2d(time_base), 1);
  503. }
  504. nut->max_distance = MAX_DISTANCE;
  505. build_elision_headers(s);
  506. build_frame_code(s);
  507. assert(nut->frame_code['N'].flags == FLAG_INVALID);
  508. put_buffer(bc, ID_STRING, strlen(ID_STRING));
  509. put_byte(bc, 0);
  510. write_headers(nut, bc);
  511. put_flush_packet(bc);
  512. //FIXME index
  513. return 0;
  514. }
  515. static int get_needed_flags(NUTContext *nut, StreamContext *nus, FrameCode *fc, AVPacket *pkt){
  516. int flags= 0;
  517. if(pkt->flags & PKT_FLAG_KEY ) flags |= FLAG_KEY;
  518. if(pkt->stream_index != fc->stream_id ) flags |= FLAG_STREAM_ID;
  519. if(pkt->size / fc->size_mul ) flags |= FLAG_SIZE_MSB;
  520. if(pkt->pts - nus->last_pts != fc->pts_delta) flags |= FLAG_CODED_PTS;
  521. if(pkt->size > 2*nut->max_distance ) flags |= FLAG_CHECKSUM;
  522. if(FFABS(pkt->pts - nus->last_pts)
  523. > nus->max_pts_distance) flags |= FLAG_CHECKSUM;
  524. if( pkt->size < nut->header_len[fc->header_idx]
  525. || (pkt->size > 4096 && fc->header_idx)
  526. || memcmp(pkt->data, nut->header[fc->header_idx], nut->header_len[fc->header_idx]))
  527. flags |= FLAG_HEADER_IDX;
  528. return flags | (fc->flags & FLAG_CODED);
  529. }
  530. static int find_best_header_idx(NUTContext *nut, AVPacket *pkt){
  531. int i;
  532. int best_i = 0;
  533. int best_len= 0;
  534. if(pkt->size > 4096)
  535. return 0;
  536. for(i=1; i<nut->header_count; i++){
  537. if( pkt->size >= nut->header_len[i]
  538. && nut->header_len[i] > best_len
  539. && !memcmp(pkt->data, nut->header[i], nut->header_len[i])){
  540. best_i= i;
  541. best_len= nut->header_len[i];
  542. }
  543. }
  544. return best_i;
  545. }
  546. static int write_packet(AVFormatContext *s, AVPacket *pkt){
  547. NUTContext *nut = s->priv_data;
  548. StreamContext *nus= &nut->stream[pkt->stream_index];
  549. ByteIOContext *bc = s->pb, *dyn_bc;
  550. FrameCode *fc;
  551. int64_t coded_pts;
  552. int best_length, frame_code, flags, needed_flags, i, header_idx, best_header_idx;
  553. int key_frame = !!(pkt->flags & PKT_FLAG_KEY);
  554. int store_sp=0;
  555. int ret;
  556. if(1LL<<(20+3*nut->header_count) <= url_ftell(bc))
  557. write_headers(nut, bc);
  558. if(key_frame && !(nus->last_flags & FLAG_KEY))
  559. store_sp= 1;
  560. if(pkt->size + 30/*FIXME check*/ + url_ftell(bc) >= nut->last_syncpoint_pos + nut->max_distance)
  561. store_sp= 1;
  562. //FIXME: Ensure store_sp is 1 in the first place.
  563. if(store_sp){
  564. syncpoint_t *sp, dummy= {.pos= INT64_MAX};
  565. ff_nut_reset_ts(nut, *nus->time_base, pkt->dts);
  566. for(i=0; i<s->nb_streams; i++){
  567. AVStream *st= s->streams[i];
  568. int64_t dts_tb = av_rescale_rnd(pkt->dts,
  569. nus->time_base->num * (int64_t)nut->stream[i].time_base->den,
  570. nus->time_base->den * (int64_t)nut->stream[i].time_base->num,
  571. AV_ROUND_DOWN);
  572. int index= av_index_search_timestamp(st, dts_tb, AVSEEK_FLAG_BACKWARD);
  573. if(index>=0) dummy.pos= FFMIN(dummy.pos, st->index_entries[index].pos);
  574. }
  575. if(dummy.pos == INT64_MAX)
  576. dummy.pos= 0;
  577. sp= av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp, NULL);
  578. nut->last_syncpoint_pos= url_ftell(bc);
  579. ret = url_open_dyn_buf(&dyn_bc);
  580. if(ret < 0)
  581. return ret;
  582. put_t(nut, nus, dyn_bc, pkt->dts);
  583. put_v(dyn_bc, sp ? (nut->last_syncpoint_pos - sp->pos)>>4 : 0);
  584. put_packet(nut, bc, dyn_bc, 1, SYNCPOINT_STARTCODE);
  585. ff_nut_add_sp(nut, nut->last_syncpoint_pos, 0/*unused*/, pkt->dts);
  586. }
  587. assert(nus->last_pts != AV_NOPTS_VALUE);
  588. coded_pts = pkt->pts & ((1<<nus->msb_pts_shift)-1);
  589. if(ff_lsb2full(nus, coded_pts) != pkt->pts)
  590. coded_pts= pkt->pts + (1<<nus->msb_pts_shift);
  591. best_header_idx= find_best_header_idx(nut, pkt);
  592. best_length=INT_MAX;
  593. frame_code= -1;
  594. for(i=0; i<256; i++){
  595. int length= 0;
  596. FrameCode *fc= &nut->frame_code[i];
  597. int flags= fc->flags;
  598. if(flags & FLAG_INVALID)
  599. continue;
  600. needed_flags= get_needed_flags(nut, nus, fc, pkt);
  601. if(flags & FLAG_CODED){
  602. length++;
  603. flags = needed_flags;
  604. }
  605. if((flags & needed_flags) != needed_flags)
  606. continue;
  607. if((flags ^ needed_flags) & FLAG_KEY)
  608. continue;
  609. if(flags & FLAG_STREAM_ID)
  610. length+= get_length(pkt->stream_index);
  611. if(pkt->size % fc->size_mul != fc->size_lsb)
  612. continue;
  613. if(flags & FLAG_SIZE_MSB)
  614. length += get_length(pkt->size / fc->size_mul);
  615. if(flags & FLAG_CHECKSUM)
  616. length+=4;
  617. if(flags & FLAG_CODED_PTS)
  618. length += get_length(coded_pts);
  619. if( (flags & FLAG_CODED)
  620. && nut->header_len[best_header_idx] > nut->header_len[fc->header_idx]+1){
  621. flags |= FLAG_HEADER_IDX;
  622. }
  623. if(flags & FLAG_HEADER_IDX){
  624. length += 1 - nut->header_len[best_header_idx];
  625. }else{
  626. length -= nut->header_len[fc->header_idx];
  627. }
  628. length*=4;
  629. length+= !(flags & FLAG_CODED_PTS);
  630. length+= !(flags & FLAG_CHECKSUM);
  631. if(length < best_length){
  632. best_length= length;
  633. frame_code=i;
  634. }
  635. }
  636. assert(frame_code != -1);
  637. fc= &nut->frame_code[frame_code];
  638. flags= fc->flags;
  639. needed_flags= get_needed_flags(nut, nus, fc, pkt);
  640. header_idx= fc->header_idx;
  641. init_checksum(bc, ff_crc04C11DB7_update, 0);
  642. put_byte(bc, frame_code);
  643. if(flags & FLAG_CODED){
  644. put_v(bc, (flags^needed_flags) & ~(FLAG_CODED));
  645. flags = needed_flags;
  646. }
  647. if(flags & FLAG_STREAM_ID) put_v(bc, pkt->stream_index);
  648. if(flags & FLAG_CODED_PTS) put_v(bc, coded_pts);
  649. if(flags & FLAG_SIZE_MSB) put_v(bc, pkt->size / fc->size_mul);
  650. if(flags & FLAG_HEADER_IDX) put_v(bc, header_idx= best_header_idx);
  651. if(flags & FLAG_CHECKSUM) put_le32(bc, get_checksum(bc));
  652. else get_checksum(bc);
  653. put_buffer(bc, pkt->data + nut->header_len[header_idx], pkt->size - nut->header_len[header_idx]);
  654. nus->last_flags= flags;
  655. nus->last_pts= pkt->pts;
  656. //FIXME just store one per syncpoint
  657. if(flags & FLAG_KEY)
  658. av_add_index_entry(
  659. s->streams[pkt->stream_index],
  660. nut->last_syncpoint_pos,
  661. pkt->pts,
  662. 0,
  663. 0,
  664. AVINDEX_KEYFRAME);
  665. return 0;
  666. }
  667. static int write_trailer(AVFormatContext *s){
  668. NUTContext *nut= s->priv_data;
  669. ByteIOContext *bc= s->pb;
  670. while(nut->header_count<3)
  671. write_headers(nut, bc);
  672. put_flush_packet(bc);
  673. return 0;
  674. }
  675. AVOutputFormat nut_muxer = {
  676. "nut",
  677. "nut format",
  678. "video/x-nut",
  679. "nut",
  680. sizeof(NUTContext),
  681. #ifdef CONFIG_LIBVORBIS
  682. CODEC_ID_VORBIS,
  683. #elif defined(CONFIG_LIBMP3LAME)
  684. CODEC_ID_MP3,
  685. #else
  686. CODEC_ID_MP2, /* AC3 needs liba52 decoder */
  687. #endif
  688. CODEC_ID_MPEG4,
  689. write_header,
  690. write_packet,
  691. write_trailer,
  692. .flags = AVFMT_GLOBALHEADER,
  693. .codec_tag= (const AVCodecTag*[]){codec_bmp_tags, codec_wav_tags, ff_nut_subtitle_tags, 0},
  694. };