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.

706 lines
16KB

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