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.

630 lines
14KB

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