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.

741 lines
17KB

  1. /*
  2. * RAW encoder and decoder
  3. * Copyright (c) 2001 Fabrice Bellard.
  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 Lesser 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. #include "avformat.h"
  20. #ifdef CONFIG_MUXERS
  21. /* simple formats */
  22. static int raw_write_header(struct AVFormatContext *s)
  23. {
  24. return 0;
  25. }
  26. static int raw_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  27. {
  28. put_buffer(&s->pb, pkt->data, pkt->size);
  29. put_flush_packet(&s->pb);
  30. return 0;
  31. }
  32. static int raw_write_trailer(struct AVFormatContext *s)
  33. {
  34. return 0;
  35. }
  36. #endif //CONFIG_MUXERS
  37. /* raw input */
  38. static int raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
  39. {
  40. AVStream *st;
  41. int id;
  42. st = av_new_stream(s, 0);
  43. if (!st)
  44. return AVERROR_NOMEM;
  45. if (ap) {
  46. id = s->iformat->value;
  47. if (id == CODEC_ID_RAWVIDEO) {
  48. st->codec->codec_type = CODEC_TYPE_VIDEO;
  49. } else {
  50. st->codec->codec_type = CODEC_TYPE_AUDIO;
  51. }
  52. st->codec->codec_id = id;
  53. switch(st->codec->codec_type) {
  54. case CODEC_TYPE_AUDIO:
  55. st->codec->sample_rate = ap->sample_rate;
  56. st->codec->channels = ap->channels;
  57. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  58. break;
  59. case CODEC_TYPE_VIDEO:
  60. av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
  61. st->codec->width = ap->width;
  62. st->codec->height = ap->height;
  63. st->codec->pix_fmt = ap->pix_fmt;
  64. if(st->codec->pix_fmt == PIX_FMT_NONE)
  65. st->codec->pix_fmt= PIX_FMT_YUV420P;
  66. break;
  67. default:
  68. return -1;
  69. }
  70. } else {
  71. return -1;
  72. }
  73. return 0;
  74. }
  75. #define RAW_PACKET_SIZE 1024
  76. static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
  77. {
  78. int ret, size;
  79. // AVStream *st = s->streams[0];
  80. size= RAW_PACKET_SIZE;
  81. ret= av_get_packet(&s->pb, pkt, size);
  82. pkt->stream_index = 0;
  83. if (ret <= 0) {
  84. return AVERROR_IO;
  85. }
  86. /* note: we need to modify the packet size here to handle the last
  87. packet */
  88. pkt->size = ret;
  89. return ret;
  90. }
  91. static int raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
  92. {
  93. int ret, size;
  94. size = RAW_PACKET_SIZE;
  95. if (av_new_packet(pkt, size) < 0)
  96. return AVERROR_IO;
  97. pkt->pos= url_ftell(&s->pb);
  98. pkt->stream_index = 0;
  99. ret = get_partial_buffer(&s->pb, pkt->data, size);
  100. if (ret <= 0) {
  101. av_free_packet(pkt);
  102. return AVERROR_IO;
  103. }
  104. pkt->size = ret;
  105. return ret;
  106. }
  107. static int raw_read_close(AVFormatContext *s)
  108. {
  109. return 0;
  110. }
  111. int pcm_read_seek(AVFormatContext *s,
  112. int stream_index, int64_t timestamp, int flags)
  113. {
  114. AVStream *st;
  115. int block_align, byte_rate;
  116. int64_t pos;
  117. st = s->streams[0];
  118. switch(st->codec->codec_id) {
  119. case CODEC_ID_PCM_S16LE:
  120. case CODEC_ID_PCM_S16BE:
  121. case CODEC_ID_PCM_U16LE:
  122. case CODEC_ID_PCM_U16BE:
  123. block_align = 2 * st->codec->channels;
  124. byte_rate = block_align * st->codec->sample_rate;
  125. break;
  126. case CODEC_ID_PCM_S8:
  127. case CODEC_ID_PCM_U8:
  128. case CODEC_ID_PCM_MULAW:
  129. case CODEC_ID_PCM_ALAW:
  130. block_align = st->codec->channels;
  131. byte_rate = block_align * st->codec->sample_rate;
  132. break;
  133. default:
  134. block_align = st->codec->block_align;
  135. byte_rate = st->codec->bit_rate / 8;
  136. break;
  137. }
  138. if (block_align <= 0 || byte_rate <= 0)
  139. return -1;
  140. /* compute the position by aligning it to block_align */
  141. pos = av_rescale_rnd(timestamp * byte_rate,
  142. st->time_base.num,
  143. st->time_base.den * (int64_t)block_align,
  144. (flags & AVSEEK_FLAG_BACKWARD) ? AV_ROUND_DOWN : AV_ROUND_UP);
  145. pos *= block_align;
  146. /* recompute exact position */
  147. st->cur_dts = av_rescale(pos, st->time_base.den, byte_rate * (int64_t)st->time_base.num);
  148. url_fseek(&s->pb, pos + s->data_offset, SEEK_SET);
  149. return 0;
  150. }
  151. /* ac3 read */
  152. static int ac3_read_header(AVFormatContext *s,
  153. AVFormatParameters *ap)
  154. {
  155. AVStream *st;
  156. st = av_new_stream(s, 0);
  157. if (!st)
  158. return AVERROR_NOMEM;
  159. st->codec->codec_type = CODEC_TYPE_AUDIO;
  160. st->codec->codec_id = CODEC_ID_AC3;
  161. st->need_parsing = 1;
  162. /* the parameters will be extracted from the compressed bitstream */
  163. return 0;
  164. }
  165. static int shorten_read_header(AVFormatContext *s,
  166. AVFormatParameters *ap)
  167. {
  168. AVStream *st;
  169. st = av_new_stream(s, 0);
  170. if (!st)
  171. return AVERROR_NOMEM;
  172. st->codec->codec_type = CODEC_TYPE_AUDIO;
  173. st->codec->codec_id = CODEC_ID_SHORTEN;
  174. st->need_parsing = 1;
  175. /* the parameters will be extracted from the compressed bitstream */
  176. return 0;
  177. }
  178. /* dts read */
  179. static int dts_read_header(AVFormatContext *s,
  180. AVFormatParameters *ap)
  181. {
  182. AVStream *st;
  183. st = av_new_stream(s, 0);
  184. if (!st)
  185. return AVERROR_NOMEM;
  186. st->codec->codec_type = CODEC_TYPE_AUDIO;
  187. st->codec->codec_id = CODEC_ID_DTS;
  188. st->need_parsing = 1;
  189. /* the parameters will be extracted from the compressed bitstream */
  190. return 0;
  191. }
  192. /* mpeg1/h263 input */
  193. static int video_read_header(AVFormatContext *s,
  194. AVFormatParameters *ap)
  195. {
  196. AVStream *st;
  197. st = av_new_stream(s, 0);
  198. if (!st)
  199. return AVERROR_NOMEM;
  200. st->codec->codec_type = CODEC_TYPE_VIDEO;
  201. st->codec->codec_id = s->iformat->value;
  202. st->need_parsing = 1;
  203. /* for mjpeg, specify frame rate */
  204. /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
  205. if (ap && ap->time_base.num) {
  206. av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
  207. } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
  208. st->codec->codec_id == CODEC_ID_MPEG4 ||
  209. st->codec->codec_id == CODEC_ID_H264) {
  210. av_set_pts_info(st, 64, 1, 25);
  211. }
  212. return 0;
  213. }
  214. #define SEQ_START_CODE 0x000001b3
  215. #define GOP_START_CODE 0x000001b8
  216. #define PICTURE_START_CODE 0x00000100
  217. /* XXX: improve that by looking at several start codes */
  218. static int mpegvideo_probe(AVProbeData *p)
  219. {
  220. int code;
  221. const uint8_t *d;
  222. /* we search the first start code. If it is a sequence, gop or
  223. picture start code then we decide it is an mpeg video
  224. stream. We do not send highest value to give a chance to mpegts */
  225. /* NOTE: the search range was restricted to avoid too many false
  226. detections */
  227. if (p->buf_size < 6)
  228. return 0;
  229. d = p->buf;
  230. code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]);
  231. if ((code & 0xffffff00) == 0x100) {
  232. if (code == SEQ_START_CODE ||
  233. code == GOP_START_CODE ||
  234. code == PICTURE_START_CODE)
  235. return 50 - 1;
  236. else
  237. return 0;
  238. }
  239. return 0;
  240. }
  241. static int h263_probe(AVProbeData *p)
  242. {
  243. int code;
  244. const uint8_t *d;
  245. if (p->buf_size < 6)
  246. return 0;
  247. d = p->buf;
  248. code = (d[0] << 14) | (d[1] << 6) | (d[2] >> 2);
  249. if (code == 0x20) {
  250. return 50;
  251. }
  252. return 0;
  253. }
  254. static int h261_probe(AVProbeData *p)
  255. {
  256. int code;
  257. const uint8_t *d;
  258. if (p->buf_size < 6)
  259. return 0;
  260. d = p->buf;
  261. code = (d[0] << 12) | (d[1] << 4) | (d[2] >> 4);
  262. if (code == 0x10) {
  263. return 50;
  264. }
  265. return 0;
  266. }
  267. AVInputFormat shorten_iformat = {
  268. "shn",
  269. "raw shn",
  270. 0,
  271. NULL,
  272. shorten_read_header,
  273. raw_read_partial_packet,
  274. raw_read_close,
  275. .extensions = "shn",
  276. };
  277. AVInputFormat ac3_iformat = {
  278. "ac3",
  279. "raw ac3",
  280. 0,
  281. NULL,
  282. ac3_read_header,
  283. raw_read_partial_packet,
  284. raw_read_close,
  285. .extensions = "ac3",
  286. };
  287. #ifdef CONFIG_MUXERS
  288. AVOutputFormat ac3_oformat = {
  289. "ac3",
  290. "raw ac3",
  291. "audio/x-ac3",
  292. "ac3",
  293. 0,
  294. CODEC_ID_AC3,
  295. 0,
  296. raw_write_header,
  297. raw_write_packet,
  298. raw_write_trailer,
  299. };
  300. #endif //CONFIG_MUXERS
  301. AVInputFormat dts_iformat = {
  302. "dts",
  303. "raw dts",
  304. 0,
  305. NULL,
  306. dts_read_header,
  307. raw_read_partial_packet,
  308. raw_read_close,
  309. .extensions = "dts",
  310. };
  311. AVInputFormat h261_iformat = {
  312. "h261",
  313. "raw h261",
  314. 0,
  315. h261_probe,
  316. video_read_header,
  317. raw_read_partial_packet,
  318. raw_read_close,
  319. .extensions = "h261",
  320. .value = CODEC_ID_H261,
  321. };
  322. #ifdef CONFIG_MUXERS
  323. AVOutputFormat h261_oformat = {
  324. "h261",
  325. "raw h261",
  326. "video/x-h261",
  327. "h261",
  328. 0,
  329. 0,
  330. CODEC_ID_H261,
  331. raw_write_header,
  332. raw_write_packet,
  333. raw_write_trailer,
  334. };
  335. #endif //CONFIG_MUXERS
  336. AVInputFormat h263_iformat = {
  337. "h263",
  338. "raw h263",
  339. 0,
  340. h263_probe,
  341. video_read_header,
  342. raw_read_partial_packet,
  343. raw_read_close,
  344. // .extensions = "h263", //FIXME remove after writing mpeg4_probe
  345. .value = CODEC_ID_H263,
  346. };
  347. #ifdef CONFIG_MUXERS
  348. AVOutputFormat h263_oformat = {
  349. "h263",
  350. "raw h263",
  351. "video/x-h263",
  352. "h263",
  353. 0,
  354. 0,
  355. CODEC_ID_H263,
  356. raw_write_header,
  357. raw_write_packet,
  358. raw_write_trailer,
  359. };
  360. #endif //CONFIG_MUXERS
  361. AVInputFormat m4v_iformat = {
  362. "m4v",
  363. "raw MPEG4 video format",
  364. 0,
  365. NULL /*mpegvideo_probe*/,
  366. video_read_header,
  367. raw_read_partial_packet,
  368. raw_read_close,
  369. .extensions = "m4v", //FIXME remove after writing mpeg4_probe
  370. .value = CODEC_ID_MPEG4,
  371. };
  372. #ifdef CONFIG_MUXERS
  373. AVOutputFormat m4v_oformat = {
  374. "m4v",
  375. "raw MPEG4 video format",
  376. NULL,
  377. "m4v",
  378. 0,
  379. CODEC_ID_NONE,
  380. CODEC_ID_MPEG4,
  381. raw_write_header,
  382. raw_write_packet,
  383. raw_write_trailer,
  384. };
  385. #endif //CONFIG_MUXERS
  386. AVInputFormat h264_iformat = {
  387. "h264",
  388. "raw H264 video format",
  389. 0,
  390. NULL /*mpegvideo_probe*/,
  391. video_read_header,
  392. raw_read_partial_packet,
  393. raw_read_close,
  394. .extensions = "h26l,h264,264", //FIXME remove after writing mpeg4_probe
  395. .value = CODEC_ID_H264,
  396. };
  397. #ifdef CONFIG_MUXERS
  398. AVOutputFormat h264_oformat = {
  399. "h264",
  400. "raw H264 video format",
  401. NULL,
  402. "h264",
  403. 0,
  404. CODEC_ID_NONE,
  405. CODEC_ID_H264,
  406. raw_write_header,
  407. raw_write_packet,
  408. raw_write_trailer,
  409. };
  410. #endif //CONFIG_MUXERS
  411. AVInputFormat mpegvideo_iformat = {
  412. "mpegvideo",
  413. "MPEG video",
  414. 0,
  415. mpegvideo_probe,
  416. video_read_header,
  417. raw_read_partial_packet,
  418. raw_read_close,
  419. .value = CODEC_ID_MPEG1VIDEO,
  420. };
  421. #ifdef CONFIG_MUXERS
  422. AVOutputFormat mpeg1video_oformat = {
  423. "mpeg1video",
  424. "MPEG video",
  425. "video/x-mpeg",
  426. "mpg,mpeg,m1v",
  427. 0,
  428. 0,
  429. CODEC_ID_MPEG1VIDEO,
  430. raw_write_header,
  431. raw_write_packet,
  432. raw_write_trailer,
  433. };
  434. #endif //CONFIG_MUXERS
  435. #ifdef CONFIG_MUXERS
  436. AVOutputFormat mpeg2video_oformat = {
  437. "mpeg2video",
  438. "MPEG2 video",
  439. NULL,
  440. "m2v",
  441. 0,
  442. 0,
  443. CODEC_ID_MPEG2VIDEO,
  444. raw_write_header,
  445. raw_write_packet,
  446. raw_write_trailer,
  447. };
  448. #endif //CONFIG_MUXERS
  449. AVInputFormat mjpeg_iformat = {
  450. "mjpeg",
  451. "MJPEG video",
  452. 0,
  453. NULL,
  454. video_read_header,
  455. raw_read_partial_packet,
  456. raw_read_close,
  457. .extensions = "mjpg,mjpeg",
  458. .value = CODEC_ID_MJPEG,
  459. };
  460. #ifdef CONFIG_MUXERS
  461. AVOutputFormat mjpeg_oformat = {
  462. "mjpeg",
  463. "MJPEG video",
  464. "video/x-mjpeg",
  465. "mjpg,mjpeg",
  466. 0,
  467. 0,
  468. CODEC_ID_MJPEG,
  469. raw_write_header,
  470. raw_write_packet,
  471. raw_write_trailer,
  472. };
  473. #endif //CONFIG_MUXERS
  474. /* pcm formats */
  475. #define PCMINPUTDEF(name, long_name, ext, codec) \
  476. AVInputFormat pcm_ ## name ## _iformat = {\
  477. #name,\
  478. long_name,\
  479. 0,\
  480. NULL,\
  481. raw_read_header,\
  482. raw_read_packet,\
  483. raw_read_close,\
  484. pcm_read_seek,\
  485. .extensions = ext,\
  486. .value = codec,\
  487. };
  488. #if !defined(CONFIG_MUXERS) && defined(CONFIG_DEMUXERS)
  489. #define PCMDEF(name, long_name, ext, codec) \
  490. PCMINPUTDEF(name, long_name, ext, codec)
  491. #else
  492. #define PCMDEF(name, long_name, ext, codec) \
  493. PCMINPUTDEF(name, long_name, ext, codec)\
  494. \
  495. AVOutputFormat pcm_ ## name ## _oformat = {\
  496. #name,\
  497. long_name,\
  498. NULL,\
  499. ext,\
  500. 0,\
  501. codec,\
  502. 0,\
  503. raw_write_header,\
  504. raw_write_packet,\
  505. raw_write_trailer,\
  506. };
  507. #endif //CONFIG_MUXERS
  508. #ifdef WORDS_BIGENDIAN
  509. #define BE_DEF(s) s
  510. #define LE_DEF(s) NULL
  511. #else
  512. #define BE_DEF(s) NULL
  513. #define LE_DEF(s) s
  514. #endif
  515. PCMDEF(s16le, "pcm signed 16 bit little endian format",
  516. LE_DEF("sw"), CODEC_ID_PCM_S16LE)
  517. PCMDEF(s16be, "pcm signed 16 bit big endian format",
  518. BE_DEF("sw"), CODEC_ID_PCM_S16BE)
  519. PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
  520. LE_DEF("uw"), CODEC_ID_PCM_U16LE)
  521. PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
  522. BE_DEF("uw"), CODEC_ID_PCM_U16BE)
  523. PCMDEF(s8, "pcm signed 8 bit format",
  524. "sb", CODEC_ID_PCM_S8)
  525. PCMDEF(u8, "pcm unsigned 8 bit format",
  526. "ub", CODEC_ID_PCM_U8)
  527. PCMDEF(mulaw, "pcm mu law format",
  528. "ul", CODEC_ID_PCM_MULAW)
  529. PCMDEF(alaw, "pcm A law format",
  530. "al", CODEC_ID_PCM_ALAW)
  531. static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
  532. {
  533. int packet_size, ret, width, height;
  534. AVStream *st = s->streams[0];
  535. width = st->codec->width;
  536. height = st->codec->height;
  537. packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  538. if (packet_size < 0)
  539. return -1;
  540. ret= av_get_packet(&s->pb, pkt, packet_size);
  541. pkt->stream_index = 0;
  542. if (ret != packet_size) {
  543. return AVERROR_IO;
  544. } else {
  545. return 0;
  546. }
  547. }
  548. AVInputFormat rawvideo_iformat = {
  549. "rawvideo",
  550. "raw video format",
  551. 0,
  552. NULL,
  553. raw_read_header,
  554. rawvideo_read_packet,
  555. raw_read_close,
  556. .extensions = "yuv,cif,qcif",
  557. .value = CODEC_ID_RAWVIDEO,
  558. };
  559. #ifdef CONFIG_MUXERS
  560. AVOutputFormat rawvideo_oformat = {
  561. "rawvideo",
  562. "raw video format",
  563. NULL,
  564. "yuv",
  565. 0,
  566. CODEC_ID_NONE,
  567. CODEC_ID_RAWVIDEO,
  568. raw_write_header,
  569. raw_write_packet,
  570. raw_write_trailer,
  571. };
  572. #endif //CONFIG_MUXERS
  573. #ifdef CONFIG_MUXERS
  574. static int null_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  575. {
  576. return 0;
  577. }
  578. AVOutputFormat null_oformat = {
  579. "null",
  580. "null video format",
  581. NULL,
  582. NULL,
  583. 0,
  584. #ifdef WORDS_BIGENDIAN
  585. CODEC_ID_PCM_S16BE,
  586. #else
  587. CODEC_ID_PCM_S16LE,
  588. #endif
  589. CODEC_ID_RAWVIDEO,
  590. raw_write_header,
  591. null_write_packet,
  592. raw_write_trailer,
  593. .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
  594. };
  595. #endif //CONFIG_MUXERS
  596. #ifndef CONFIG_MUXERS
  597. #define av_register_output_format(format)
  598. #endif
  599. #ifndef CONFIG_DEMUXERS
  600. #define av_register_input_format(format)
  601. #endif
  602. int raw_init(void)
  603. {
  604. av_register_input_format(&shorten_iformat);
  605. av_register_input_format(&ac3_iformat);
  606. av_register_output_format(&ac3_oformat);
  607. av_register_input_format(&dts_iformat);
  608. av_register_input_format(&h261_iformat);
  609. av_register_output_format(&h261_oformat);
  610. av_register_input_format(&h263_iformat);
  611. av_register_output_format(&h263_oformat);
  612. av_register_input_format(&m4v_iformat);
  613. av_register_output_format(&m4v_oformat);
  614. av_register_input_format(&h264_iformat);
  615. av_register_output_format(&h264_oformat);
  616. av_register_input_format(&mpegvideo_iformat);
  617. av_register_output_format(&mpeg1video_oformat);
  618. av_register_output_format(&mpeg2video_oformat);
  619. av_register_input_format(&mjpeg_iformat);
  620. av_register_output_format(&mjpeg_oformat);
  621. av_register_input_format(&pcm_s16le_iformat);
  622. av_register_output_format(&pcm_s16le_oformat);
  623. av_register_input_format(&pcm_s16be_iformat);
  624. av_register_output_format(&pcm_s16be_oformat);
  625. av_register_input_format(&pcm_u16le_iformat);
  626. av_register_output_format(&pcm_u16le_oformat);
  627. av_register_input_format(&pcm_u16be_iformat);
  628. av_register_output_format(&pcm_u16be_oformat);
  629. av_register_input_format(&pcm_s8_iformat);
  630. av_register_output_format(&pcm_s8_oformat);
  631. av_register_input_format(&pcm_u8_iformat);
  632. av_register_output_format(&pcm_u8_oformat);
  633. av_register_input_format(&pcm_mulaw_iformat);
  634. av_register_output_format(&pcm_mulaw_oformat);
  635. av_register_input_format(&pcm_alaw_iformat);
  636. av_register_output_format(&pcm_alaw_oformat);
  637. av_register_input_format(&rawvideo_iformat);
  638. av_register_output_format(&rawvideo_oformat);
  639. av_register_output_format(&null_oformat);
  640. return 0;
  641. }