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.

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