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.

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