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.

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