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.

656 lines
15KB

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