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.

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