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.

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