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.

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