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.

556 lines
13KB

  1. /*
  2. * NUT (de)muxer based on initial draft
  3. * Copyright (c) 2003 Alex Beregszaszi
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * NUT DRAFT can be found in MPlayer CVS at DOCS/tech/mpcf.txt
  20. *
  21. * Compatible with draft version 20030906
  22. *
  23. */
  24. /*
  25. * TODO:
  26. * - checksumming
  27. * - correct rate denom/nom and sample_mul
  28. * - correct timestamp handling
  29. * - correct startcodes
  30. * - index writing
  31. * - info and index packet reading support
  32. * - startcode searching for broken streams
  33. * - subpacket support
  34. * - handling of codec specific headers
  35. */
  36. //#define DEBUG 1
  37. #include "avformat.h"
  38. #include "mpegaudio.h"
  39. typedef struct {
  40. int curr_frame_start;
  41. int last_frame_size;
  42. int curr_frame_size;
  43. } NUTContext;
  44. static int bytes_left(ByteIOContext *bc)
  45. {
  46. return bc->buf_end - bc->buf_ptr;
  47. }
  48. static uint64_t get_v(ByteIOContext *bc)
  49. {
  50. uint64_t val = 0;
  51. // for (; bytes_left(s)*8 > 0; )
  52. for(; bytes_left(bc) > 0; )
  53. {
  54. int tmp = get_byte(bc);
  55. if (tmp&0x80)
  56. val= (val<<7) + tmp - 0x80;
  57. else
  58. return (val<<7) + tmp;
  59. }
  60. return -1;
  61. }
  62. static int64_t get_s(ByteIOContext *bc)
  63. {
  64. int64_t v = get_v(bc) + 1;
  65. if (v&1)
  66. return -(v>>1);
  67. else
  68. return (v>>1);
  69. }
  70. static int get_b(ByteIOContext *bc, char *data, int maxlen)
  71. {
  72. int i, len;
  73. len = get_v(bc);
  74. for (i = 0; i < len && i < maxlen; i++)
  75. data[i] = get_byte(bc);
  76. if (i < len)
  77. {
  78. len = i;
  79. for (i = 0; i < len; i++)
  80. get_byte(bc);
  81. }
  82. return 0;
  83. }
  84. static int get_packetheader(NUTContext *nut, ByteIOContext *bc)
  85. {
  86. nut->curr_frame_start = url_ftell(bc);
  87. nut->curr_frame_size = get_v(bc);
  88. nut->last_frame_size = get_v(bc);
  89. dprintf("Packet: fwd: %d bwd: %d\n",
  90. nut->curr_frame_size, nut->last_frame_size);
  91. return 0;
  92. }
  93. static int get_padding(NUTContext *nut, ByteIOContext *bc)
  94. {
  95. int i, tmp, len = nut->curr_frame_size - (url_ftell(bc) - nut->curr_frame_start);
  96. for (i = 0; i < len; i++)
  97. {
  98. tmp = get_byte(bc);
  99. if (tmp != 0)
  100. fprintf(stderr, "bad padding\n");
  101. }
  102. return 0;
  103. }
  104. static int put_v(ByteIOContext *bc, uint64_t val)
  105. {
  106. int i;
  107. // if (bytes_left(s)*8 < 9)
  108. // return -1;
  109. if (bytes_left(bc) < 1)
  110. return -1;
  111. val &= 0x7FFFFFFFFFFFFFFFULL; // FIXME can only encode upto 63 bits currently
  112. for (i=7; ; i+=7)
  113. if ((val>>i) == 0)
  114. break;
  115. for (i-=7; i>0; i-=8)
  116. put_byte(bc, 0x80 | (val>>i));
  117. put_byte(bc, val&0x7f);
  118. return 0;
  119. }
  120. static int put_s(ByteIOContext *bc, uint64_t val)
  121. {
  122. if (val<=0)
  123. return put_v(bc, -2*val);
  124. else
  125. return put_v(bc, 2*val-1);
  126. }
  127. static int put_b(ByteIOContext *bc, char *data, int len)
  128. {
  129. int i;
  130. put_v(bc, len);
  131. for (i = 0; i < len; i++)
  132. put_byte(bc, data[i]);
  133. return 0;
  134. }
  135. static int put_packetheader(NUTContext *nut, ByteIOContext *bc, int est_size)
  136. {
  137. put_flush_packet(bc);
  138. nut->curr_frame_start = url_ftell(bc);
  139. nut->curr_frame_size = est_size;
  140. /* packet header */
  141. put_v(bc, nut->curr_frame_size); /* forward ptr */
  142. put_v(bc, nut->last_frame_size); /* backward ptr */
  143. dprintf("Packet: fwd: %d, bwd: %d\n",
  144. nut->curr_frame_size, nut->last_frame_size);
  145. nut->last_frame_size = nut->curr_frame_size;
  146. return 0;
  147. }
  148. static int put_padding(NUTContext *nut, ByteIOContext *bc)
  149. {
  150. int i, len = nut->curr_frame_size - (url_ftell(bc) - nut->curr_frame_start);
  151. put_flush_packet(bc);
  152. for (i = 0; i < len; i++)
  153. put_byte(bc, 0);
  154. dprintf("padded %d bytes\n", i);
  155. return 0;
  156. }
  157. static int nut_write_header(AVFormatContext *s)
  158. {
  159. NUTContext *nut = s->priv_data;
  160. ByteIOContext *bc = &s->pb;
  161. AVCodecContext *codec;
  162. int i;
  163. int stream_length = 0;
  164. for (i = 0; i < s->nb_streams; i++)
  165. {
  166. if (stream_length < (s->streams[i]->duration * (AV_TIME_BASE / 1000)))
  167. stream_length = s->streams[i]->duration * (AV_TIME_BASE / 1000);
  168. }
  169. put_packetheader(nut, bc, 16); /* FIXME: estimation */
  170. /* main header */
  171. put_le64(bc, 1); /* FIXME: unique startcode */
  172. put_v(bc, 0); /* version */
  173. put_v(bc, s->nb_streams);
  174. put_v(bc, 0); /* file size */
  175. put_v(bc, stream_length); /* len in msec */
  176. put_padding(nut, bc);
  177. put_le32(bc, 0); /* FIXME: checksum */
  178. /* stream headers */
  179. for (i = 0; i < s->nb_streams; i++)
  180. {
  181. codec = &s->streams[i]->codec;
  182. put_packetheader(nut, bc, 64); /* FIXME: estimation */
  183. put_le64(bc, 1); /* FIXME: unique startcode */
  184. put_v(bc, s->streams[i]->index);
  185. put_v(bc, (codec->codec_type == CODEC_TYPE_AUDIO) ? 32 : 0);
  186. if (codec->codec_tag)
  187. put_b(bc, codec->codec_tag, 4);
  188. else if (codec->codec_type == CODEC_TYPE_VIDEO)
  189. {
  190. int tmp = codec_get_bmp_tag(codec->codec_id);
  191. put_b(bc, &tmp, 4);
  192. // put_v(bc, 4); /* len */
  193. // put_le32(bc, codec_get_bmp_tag(codec->codec_id));
  194. }
  195. else if (codec->codec_type == CODEC_TYPE_AUDIO)
  196. {
  197. int tmp = codec_get_wav_tag(codec->codec_id);
  198. put_b(bc, &tmp, 4);
  199. // put_v(bc, 4); /* len */
  200. // put_le32(bc, codec_get_wav_tag(codec->codec_id));
  201. }
  202. put_v(bc, codec->bit_rate);
  203. put_v(bc, 0); /* no language code */
  204. put_v(bc, codec->frame_rate_base);
  205. put_v(bc, codec->frame_rate);
  206. put_v(bc, 0); /* timestamp_shift */
  207. put_v(bc, 0); /* shuffle type */
  208. put_byte(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
  209. put_v(bc, 0); /* no codec specific headers */
  210. switch(codec->codec_type)
  211. {
  212. case CODEC_TYPE_AUDIO:
  213. put_v(bc, codec->sample_rate / (double)(codec->frame_rate_base / codec->frame_rate));
  214. put_v(bc, codec->channels);
  215. put_padding(nut, bc);
  216. put_le32(bc, 0); /* FIXME: checksum */
  217. break;
  218. case CODEC_TYPE_VIDEO:
  219. put_v(bc, codec->width);
  220. put_v(bc, codec->height);
  221. put_v(bc, 0); /* aspected w */
  222. put_v(bc, 0); /* aspected h */
  223. put_v(bc, 0); /* csp type -- unknown */
  224. put_padding(nut, bc);
  225. put_le32(bc, 0); /* FIXME: checksum */
  226. break;
  227. }
  228. }
  229. #if 0
  230. /* info header */
  231. put_packetheader(nut, bc, 16+strlen(s->author)+strlen(s->title)+
  232. strlen(s->comment)+strlen(s->copyright)); /* FIXME: estimation */
  233. put_le64(bc, 1); /* FIXME: unique startcode */
  234. if (s->author[0])
  235. {
  236. put_v(bc, 5); /* type */
  237. put_b(bc, s->author, strlen(s->author));
  238. }
  239. if (s->title[0])
  240. {
  241. put_v(bc, 6); /* type */
  242. put_b(bc, s->title, strlen(s->title));
  243. }
  244. if (s->comment[0])
  245. {
  246. put_v(bc, 7); /* type */
  247. put_b(bc, s->comment, strlen(s->comment));
  248. }
  249. if (s->copyright[0])
  250. {
  251. put_v(bc, 8); /* type */
  252. put_b(bc, s->copyright, strlen(s->copyright));
  253. }
  254. /* encoder */
  255. put_v(bc, 9); /* type */
  256. put_b(bc, LIBAVFORMAT_IDENT, strlen(LIBAVFORMAT_IDENT));
  257. put_padding(nut, bc);
  258. put_le32(bc, 0); /* FIXME: checksum */
  259. #endif
  260. put_flush_packet(bc);
  261. return 0;
  262. }
  263. static int nut_write_packet(AVFormatContext *s, int stream_index,
  264. uint8_t *buf, int size, int force_pts)
  265. {
  266. NUTContext *nut = s->priv_data;
  267. ByteIOContext *bc = &s->pb;
  268. int key_frame = 0;
  269. AVCodecContext *enc;
  270. if (stream_index > s->nb_streams)
  271. return 1;
  272. enc = &s->streams[stream_index]->codec;
  273. if (enc->codec_type == CODEC_TYPE_VIDEO)
  274. key_frame = enc->coded_frame->key_frame;
  275. put_packetheader(nut, bc, size+(key_frame?16:8)+4); /* FIXME: estimation */
  276. if (key_frame)
  277. put_le64(bc, 1); /* FIXME: unique startcode */
  278. put_byte(bc, (key_frame ? 1<<5 : 0) + (1 << 1)); /* flags */
  279. put_v(bc, stream_index);
  280. put_s(bc, 0); /* lsb_timestamp */
  281. put_buffer(bc, buf, size);
  282. put_padding(nut, bc);
  283. put_flush_packet(bc);
  284. return 0;
  285. }
  286. static int nut_write_trailer(AVFormatContext *s)
  287. {
  288. NUTContext *nut = s->priv_data;
  289. ByteIOContext *bc = &s->pb;
  290. #if 0
  291. int i;
  292. /* WRITE INDEX */
  293. for (i = 0; s->nb_streams; i++)
  294. {
  295. put_packetheader(nut, bc, 64); /* FIXME: estimation */
  296. put_le64(bc, 1); /* FIXME: unique startcode */
  297. put_v(bc, s->streams[i]->id);
  298. put_v(bc, ...);
  299. put_padding(nut, bc);
  300. put_le32(bc, 0); /* FIXME: checksum */
  301. }
  302. #endif
  303. put_flush_packet(bc);
  304. return 0;
  305. }
  306. static int nut_probe(AVProbeData *p)
  307. {
  308. return AVPROBE_SCORE_MAX;
  309. }
  310. static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
  311. {
  312. NUTContext *nut = s->priv_data;
  313. ByteIOContext *bc = &s->pb;
  314. int tmp;
  315. int cur_stream, nb_streams;
  316. /* main header */
  317. get_packetheader(nut, bc);
  318. tmp = get_le64(bc);
  319. if (tmp != 1)
  320. fprintf(stderr, "damaged? startcode!=1 (%d)\n", tmp);
  321. tmp = get_v(bc);
  322. if (tmp != 0)
  323. fprintf(stderr, "bad version (%d)\n", tmp);
  324. nb_streams = get_v(bc);
  325. s->file_size = get_v(bc);
  326. s->duration = get_v(bc) / (AV_TIME_BASE / 1000);
  327. get_padding(nut, bc);
  328. get_le32(bc); /* checkusm */
  329. s->bit_rate = 0;
  330. /* stream header */
  331. for (cur_stream = 0; cur_stream < nb_streams; cur_stream++)
  332. {
  333. int class;
  334. AVStream *st;
  335. get_packetheader(nut, bc);
  336. tmp = get_le64(bc);
  337. if (tmp != 1)
  338. fprintf(stderr, "damaged? startcode!=1 (%d)\n", tmp);
  339. st = av_new_stream(s, get_v(bc));
  340. if (!st)
  341. return AVERROR_NOMEM;
  342. class = get_v(bc);
  343. switch(class)
  344. {
  345. case 0:
  346. st->codec.codec_type = CODEC_TYPE_VIDEO;
  347. // get_v(bc);
  348. // tmp = get_le32(bc);
  349. get_b(bc, &tmp, 4);
  350. st->codec.codec_id = codec_get_bmp_id(tmp);
  351. if (st->codec.codec_id == CODEC_ID_NONE)
  352. fprintf(stderr, "Unknown codec?!\n");
  353. break;
  354. case 32:
  355. st->codec.codec_type = CODEC_TYPE_AUDIO;
  356. // tmp = get_v(bc);
  357. // tmp = get_le32(bc);
  358. get_b(bc, &tmp, 4);
  359. st->codec.codec_id = codec_get_wav_id(tmp);
  360. if (st->codec.codec_id == CODEC_ID_NONE)
  361. fprintf(stderr, "Unknown codec?!\n");
  362. break;
  363. default:
  364. fprintf(stderr, "Unknown stream class (%d)\n", class);
  365. return -1;
  366. }
  367. s->bit_rate += get_v(bc);
  368. tmp = get_v(bc); /* language code */
  369. while(tmp--)
  370. get_byte(bc);
  371. st->codec.frame_rate_base = get_v(bc);
  372. st->codec.frame_rate = get_v(bc);
  373. get_v(bc); /* FIXME: msb timestamp base */
  374. get_v(bc); /* shuffle type */
  375. get_byte(bc); /* flags */
  376. get_v(bc); /* FIXME: codec specific data headers */
  377. if (class == 0) /* VIDEO */
  378. {
  379. st->codec.width = get_v(bc);
  380. st->codec.height = get_v(bc);
  381. get_v(bc); /* aspected w */
  382. get_v(bc); /* aspected h */
  383. get_v(bc); /* csp type */
  384. get_padding(nut, bc);
  385. get_le32(bc); /* checksum */
  386. }
  387. if (class == 32) /* AUDIO */
  388. {
  389. st->codec.sample_rate = get_v(bc) * (double)(st->codec.frame_rate_base / st->codec.frame_rate);
  390. st->codec.channels = get_v(bc);
  391. get_padding(nut, bc);
  392. get_le32(bc); /* checksum */
  393. }
  394. }
  395. return 0;
  396. }
  397. static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
  398. {
  399. NUTContext *nut = s->priv_data;
  400. ByteIOContext *bc = &s->pb;
  401. int tmp, id, timestamp, size;
  402. int key_frame = 0;
  403. get_packetheader(nut, bc);
  404. if (url_feof(bc))
  405. return -1;
  406. tmp = get_byte(bc);
  407. if ((tmp & 0x7f) == 1) /* zero bit set? */
  408. {
  409. tmp = get_le32(bc)+get_le16(bc)+get_byte(bc);
  410. if (!tmp)
  411. {
  412. key_frame = 1;
  413. tmp = get_byte(bc); /* flags */
  414. }
  415. else
  416. fprintf(stderr, "error in zero bit / startcode\n");
  417. }
  418. if ((tmp & 0x9f) > 3) /* priority <= 3 */
  419. fprintf(stderr, "sanity check failed!\n");
  420. id = get_v(bc);
  421. timestamp = get_s(bc);
  422. size = (nut->curr_frame_size - (url_ftell(bc)-nut->curr_frame_start));
  423. dprintf("flags: 0x%x, timestamp: %d, packet size: %d\n", tmp, timestamp, size);
  424. if (size < 0)
  425. return -1;
  426. av_new_packet(pkt, size);
  427. get_buffer(bc, pkt->data, size);
  428. pkt->stream_index = id;
  429. if (key_frame)
  430. pkt->flags |= PKT_FLAG_KEY;
  431. pkt->pts = timestamp;
  432. return 0;
  433. }
  434. static AVInputFormat nut_iformat = {
  435. "nut",
  436. "nut format",
  437. sizeof(NUTContext),
  438. nut_probe,
  439. nut_read_header,
  440. nut_read_packet,
  441. // nut_read_close,
  442. // nut_read_seek,
  443. .extensions = "nut",
  444. };
  445. static AVOutputFormat nut_oformat = {
  446. "nut",
  447. "nut format",
  448. "video/x-nut",
  449. "nut",
  450. sizeof(NUTContext),
  451. #ifdef CONFIG_VORBIS
  452. CODEC_ID_VORBIS,
  453. #elif defined(CONFIG_MP3LAME)
  454. CODEC_ID_MP3LAME,
  455. #else
  456. CODEC_ID_MP2,
  457. #endif
  458. CODEC_ID_MPEG4,
  459. nut_write_header,
  460. nut_write_packet,
  461. nut_write_trailer,
  462. };
  463. int nut_init(void)
  464. {
  465. av_register_input_format(&nut_iformat);
  466. av_register_output_format(&nut_oformat);
  467. return 0;
  468. }