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.

555 lines
12KB

  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. /* simple formats */
  21. static int raw_write_header(struct AVFormatContext *s)
  22. {
  23. return 0;
  24. }
  25. static int raw_write_packet(struct AVFormatContext *s, int stream_index,
  26. unsigned char *buf, int size, int force_pts)
  27. {
  28. put_buffer(&s->pb, buf, 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. /* raw input */
  37. static int raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
  38. {
  39. AVStream *st;
  40. int id;
  41. st = av_new_stream(s, 0);
  42. if (!st)
  43. return AVERROR_NOMEM;
  44. if (ap) {
  45. id = s->iformat->value;
  46. if (id == CODEC_ID_RAWVIDEO) {
  47. st->codec.codec_type = CODEC_TYPE_VIDEO;
  48. } else {
  49. st->codec.codec_type = CODEC_TYPE_AUDIO;
  50. }
  51. st->codec.codec_id = id;
  52. switch(st->codec.codec_type) {
  53. case CODEC_TYPE_AUDIO:
  54. st->codec.sample_rate = ap->sample_rate;
  55. st->codec.channels = ap->channels;
  56. break;
  57. case CODEC_TYPE_VIDEO:
  58. st->codec.frame_rate = ap->frame_rate;
  59. st->codec.frame_rate_base = ap->frame_rate_base;
  60. st->codec.width = ap->width;
  61. st->codec.height = ap->height;
  62. break;
  63. default:
  64. return -1;
  65. }
  66. } else {
  67. return -1;
  68. }
  69. return 0;
  70. }
  71. #define RAW_PACKET_SIZE 1024
  72. static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
  73. {
  74. int ret, size;
  75. // AVStream *st = s->streams[0];
  76. size= RAW_PACKET_SIZE;
  77. if (av_new_packet(pkt, size) < 0)
  78. return -EIO;
  79. pkt->stream_index = 0;
  80. ret = get_buffer(&s->pb, pkt->data, size);
  81. if (ret <= 0) {
  82. av_free_packet(pkt);
  83. return -EIO;
  84. }
  85. /* note: we need to modify the packet size here to handle the last
  86. packet */
  87. pkt->size = ret;
  88. return ret;
  89. }
  90. static int raw_read_close(AVFormatContext *s)
  91. {
  92. return 0;
  93. }
  94. /* mp3 read */
  95. static int mp3_read_header(AVFormatContext *s,
  96. AVFormatParameters *ap)
  97. {
  98. AVStream *st;
  99. st = av_new_stream(s, 0);
  100. if (!st)
  101. return AVERROR_NOMEM;
  102. st->codec.codec_type = CODEC_TYPE_AUDIO;
  103. st->codec.codec_id = CODEC_ID_MP2;
  104. /* the parameters will be extracted from the compressed bitstream */
  105. return 0;
  106. }
  107. /* ac3 read */
  108. static int ac3_read_header(AVFormatContext *s,
  109. AVFormatParameters *ap)
  110. {
  111. AVStream *st;
  112. st = av_new_stream(s, 0);
  113. if (!st)
  114. return AVERROR_NOMEM;
  115. st->codec.codec_type = CODEC_TYPE_AUDIO;
  116. st->codec.codec_id = CODEC_ID_AC3;
  117. /* the parameters will be extracted from the compressed bitstream */
  118. return 0;
  119. }
  120. /* mpeg1/h263 input */
  121. static int video_read_header(AVFormatContext *s,
  122. AVFormatParameters *ap)
  123. {
  124. AVStream *st;
  125. st = av_new_stream(s, 0);
  126. if (!st)
  127. return AVERROR_NOMEM;
  128. st->codec.codec_type = CODEC_TYPE_VIDEO;
  129. st->codec.codec_id = s->iformat->value;
  130. /* for mjpeg, specify frame rate */
  131. /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
  132. if (st->codec.codec_id == CODEC_ID_MJPEG || st->codec.codec_id == CODEC_ID_MPEG4) {
  133. if (ap) {
  134. st->codec.frame_rate = ap->frame_rate;
  135. st->codec.frame_rate_base = ap->frame_rate_base;
  136. } else {
  137. st->codec.frame_rate = 25;
  138. st->codec.frame_rate_base = 1;
  139. }
  140. }
  141. return 0;
  142. }
  143. #define SEQ_START_CODE 0x000001b3
  144. #define GOP_START_CODE 0x000001b8
  145. #define PICTURE_START_CODE 0x00000100
  146. /* XXX: improve that by looking at several start codes */
  147. static int mpegvideo_probe(AVProbeData *p)
  148. {
  149. int code;
  150. const uint8_t *d;
  151. /* we search the first start code. If it is a sequence, gop or
  152. picture start code then we decide it is an mpeg video
  153. stream. We do not send highest value to give a chance to mpegts */
  154. /* NOTE: the search range was restricted to avoid too many false
  155. detections */
  156. if (p->buf_size < 6)
  157. return 0;
  158. d = p->buf;
  159. code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]);
  160. if ((code & 0xffffff00) == 0x100) {
  161. if (code == SEQ_START_CODE ||
  162. code == GOP_START_CODE ||
  163. code == PICTURE_START_CODE)
  164. return 50 - 1;
  165. else
  166. return 0;
  167. }
  168. return 0;
  169. }
  170. AVInputFormat mp3_iformat = {
  171. "mp3",
  172. "MPEG audio",
  173. 0,
  174. NULL,
  175. mp3_read_header,
  176. raw_read_packet,
  177. raw_read_close,
  178. .extensions = "mp2,mp3", /* XXX: use probe */
  179. };
  180. AVOutputFormat mp2_oformat = {
  181. "mp2",
  182. "MPEG audio layer 2",
  183. "audio/x-mpeg",
  184. "mp2,mp3",
  185. 0,
  186. CODEC_ID_MP2,
  187. 0,
  188. raw_write_header,
  189. raw_write_packet,
  190. raw_write_trailer,
  191. };
  192. AVInputFormat ac3_iformat = {
  193. "ac3",
  194. "raw ac3",
  195. 0,
  196. NULL,
  197. ac3_read_header,
  198. raw_read_packet,
  199. raw_read_close,
  200. .extensions = "ac3",
  201. };
  202. AVOutputFormat ac3_oformat = {
  203. "ac3",
  204. "raw ac3",
  205. "audio/x-ac3",
  206. "ac3",
  207. 0,
  208. CODEC_ID_AC3,
  209. 0,
  210. raw_write_header,
  211. raw_write_packet,
  212. raw_write_trailer,
  213. };
  214. AVOutputFormat h263_oformat = {
  215. "h263",
  216. "raw h263",
  217. "video/x-h263",
  218. "h263",
  219. 0,
  220. 0,
  221. CODEC_ID_H263,
  222. raw_write_header,
  223. raw_write_packet,
  224. raw_write_trailer,
  225. };
  226. AVInputFormat m4v_iformat = {
  227. "m4v",
  228. "raw MPEG4 video format",
  229. 0,
  230. NULL /*mpegvideo_probe*/,
  231. video_read_header,
  232. raw_read_packet,
  233. raw_read_close,
  234. .extensions = "m4v", //FIXME remove after writing mpeg4_probe
  235. .value = CODEC_ID_MPEG4,
  236. };
  237. AVOutputFormat m4v_oformat = {
  238. "m4v",
  239. "raw MPEG4 video format",
  240. NULL,
  241. "m4v",
  242. 0,
  243. CODEC_ID_NONE,
  244. CODEC_ID_MPEG4,
  245. raw_write_header,
  246. raw_write_packet,
  247. raw_write_trailer,
  248. };
  249. AVInputFormat h264_iformat = {
  250. "h264",
  251. "raw H264 video format",
  252. 0,
  253. NULL /*mpegvideo_probe*/,
  254. video_read_header,
  255. raw_read_packet,
  256. raw_read_close,
  257. .extensions = "h26l,h264", //FIXME remove after writing mpeg4_probe
  258. .value = CODEC_ID_H264,
  259. };
  260. AVOutputFormat h264_oformat = {
  261. "h264",
  262. "raw H264 video format",
  263. NULL,
  264. "h264",
  265. 0,
  266. CODEC_ID_NONE,
  267. CODEC_ID_H264,
  268. raw_write_header,
  269. raw_write_packet,
  270. raw_write_trailer,
  271. };
  272. AVInputFormat mpegvideo_iformat = {
  273. "mpegvideo",
  274. "MPEG video",
  275. 0,
  276. mpegvideo_probe,
  277. video_read_header,
  278. raw_read_packet,
  279. raw_read_close,
  280. .value = CODEC_ID_MPEG1VIDEO,
  281. };
  282. AVOutputFormat mpeg1video_oformat = {
  283. "mpeg1video",
  284. "MPEG video",
  285. "video/x-mpeg",
  286. "mpg,mpeg",
  287. 0,
  288. 0,
  289. CODEC_ID_MPEG1VIDEO,
  290. raw_write_header,
  291. raw_write_packet,
  292. raw_write_trailer,
  293. };
  294. AVInputFormat mjpeg_iformat = {
  295. "mjpeg",
  296. "MJPEG video",
  297. 0,
  298. NULL,
  299. video_read_header,
  300. raw_read_packet,
  301. raw_read_close,
  302. .extensions = "mjpg,mjpeg",
  303. .value = CODEC_ID_MJPEG,
  304. };
  305. AVOutputFormat mjpeg_oformat = {
  306. "mjpeg",
  307. "MJPEG video",
  308. "video/x-mjpeg",
  309. "mjpg,mjpeg",
  310. 0,
  311. 0,
  312. CODEC_ID_MJPEG,
  313. raw_write_header,
  314. raw_write_packet,
  315. raw_write_trailer,
  316. };
  317. /* pcm formats */
  318. #define PCMDEF(name, long_name, ext, codec) \
  319. AVInputFormat pcm_ ## name ## _iformat = {\
  320. #name,\
  321. long_name,\
  322. 0,\
  323. NULL,\
  324. raw_read_header,\
  325. raw_read_packet,\
  326. raw_read_close,\
  327. .extensions = ext,\
  328. .value = codec,\
  329. };\
  330. \
  331. AVOutputFormat pcm_ ## name ## _oformat = {\
  332. #name,\
  333. long_name,\
  334. NULL,\
  335. ext,\
  336. 0,\
  337. codec,\
  338. 0,\
  339. raw_write_header,\
  340. raw_write_packet,\
  341. raw_write_trailer,\
  342. };
  343. #ifdef WORDS_BIGENDIAN
  344. #define BE_DEF(s) s
  345. #define LE_DEF(s) NULL
  346. #else
  347. #define BE_DEF(s) NULL
  348. #define LE_DEF(s) s
  349. #endif
  350. PCMDEF(s16le, "pcm signed 16 bit little endian format",
  351. LE_DEF("sw"), CODEC_ID_PCM_S16LE)
  352. PCMDEF(s16be, "pcm signed 16 bit big endian format",
  353. BE_DEF("sw"), CODEC_ID_PCM_S16BE)
  354. PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
  355. LE_DEF("uw"), CODEC_ID_PCM_U16LE)
  356. PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
  357. BE_DEF("uw"), CODEC_ID_PCM_U16BE)
  358. PCMDEF(s8, "pcm signed 8 bit format",
  359. "sb", CODEC_ID_PCM_S8)
  360. PCMDEF(u8, "pcm unsigned 8 bit format",
  361. "ub", CODEC_ID_PCM_U8)
  362. PCMDEF(mulaw, "pcm mu law format",
  363. "ul", CODEC_ID_PCM_MULAW)
  364. PCMDEF(alaw, "pcm A law format",
  365. "al", CODEC_ID_PCM_ALAW)
  366. static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
  367. {
  368. int packet_size, ret, width, height;
  369. AVStream *st = s->streams[0];
  370. width = st->codec.width;
  371. height = st->codec.height;
  372. switch(st->codec.pix_fmt) {
  373. case PIX_FMT_YUV420P:
  374. packet_size = (width * height * 3) / 2;
  375. break;
  376. case PIX_FMT_YUV422:
  377. packet_size = (width * height * 2);
  378. break;
  379. case PIX_FMT_BGR24:
  380. case PIX_FMT_RGB24:
  381. packet_size = (width * height * 3);
  382. break;
  383. default:
  384. av_abort();
  385. break;
  386. }
  387. if (av_new_packet(pkt, packet_size) < 0)
  388. return -EIO;
  389. pkt->stream_index = 0;
  390. #if 0
  391. /* bypass buffered I/O */
  392. ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
  393. #else
  394. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  395. #endif
  396. if (ret != pkt->size) {
  397. av_free_packet(pkt);
  398. return -EIO;
  399. } else {
  400. return 0;
  401. }
  402. }
  403. AVInputFormat rawvideo_iformat = {
  404. "rawvideo",
  405. "raw video format",
  406. 0,
  407. NULL,
  408. raw_read_header,
  409. rawvideo_read_packet,
  410. raw_read_close,
  411. .extensions = "yuv",
  412. .value = CODEC_ID_RAWVIDEO,
  413. };
  414. AVOutputFormat rawvideo_oformat = {
  415. "rawvideo",
  416. "raw video format",
  417. NULL,
  418. "yuv",
  419. 0,
  420. CODEC_ID_NONE,
  421. CODEC_ID_RAWVIDEO,
  422. raw_write_header,
  423. raw_write_packet,
  424. raw_write_trailer,
  425. };
  426. static int null_write_packet(struct AVFormatContext *s,
  427. int stream_index,
  428. unsigned char *buf, int size, int force_pts)
  429. {
  430. return 0;
  431. }
  432. AVOutputFormat null_oformat = {
  433. "null",
  434. "null video format",
  435. NULL,
  436. NULL,
  437. 0,
  438. #ifdef WORDS_BIGENDIAN
  439. CODEC_ID_PCM_S16BE,
  440. #else
  441. CODEC_ID_PCM_S16LE,
  442. #endif
  443. CODEC_ID_RAWVIDEO,
  444. raw_write_header,
  445. null_write_packet,
  446. raw_write_trailer,
  447. .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
  448. };
  449. int raw_init(void)
  450. {
  451. av_register_input_format(&mp3_iformat);
  452. av_register_output_format(&mp2_oformat);
  453. av_register_input_format(&ac3_iformat);
  454. av_register_output_format(&ac3_oformat);
  455. av_register_output_format(&h263_oformat);
  456. av_register_input_format(&m4v_iformat);
  457. av_register_output_format(&m4v_oformat);
  458. av_register_input_format(&h264_iformat);
  459. av_register_output_format(&h264_oformat);
  460. av_register_input_format(&mpegvideo_iformat);
  461. av_register_output_format(&mpeg1video_oformat);
  462. av_register_input_format(&mjpeg_iformat);
  463. av_register_output_format(&mjpeg_oformat);
  464. av_register_input_format(&pcm_s16le_iformat);
  465. av_register_output_format(&pcm_s16le_oformat);
  466. av_register_input_format(&pcm_s16be_iformat);
  467. av_register_output_format(&pcm_s16be_oformat);
  468. av_register_input_format(&pcm_u16le_iformat);
  469. av_register_output_format(&pcm_u16le_oformat);
  470. av_register_input_format(&pcm_u16be_iformat);
  471. av_register_output_format(&pcm_u16be_oformat);
  472. av_register_input_format(&pcm_s8_iformat);
  473. av_register_output_format(&pcm_s8_oformat);
  474. av_register_input_format(&pcm_u8_iformat);
  475. av_register_output_format(&pcm_u8_oformat);
  476. av_register_input_format(&pcm_mulaw_iformat);
  477. av_register_output_format(&pcm_mulaw_oformat);
  478. av_register_input_format(&pcm_alaw_iformat);
  479. av_register_output_format(&pcm_alaw_oformat);
  480. av_register_input_format(&rawvideo_iformat);
  481. av_register_output_format(&rawvideo_oformat);
  482. av_register_output_format(&null_oformat);
  483. return 0;
  484. }