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.

510 lines
11KB

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