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.

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