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.

447 lines
10KB

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