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.

513 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;
  137. const uint8_t *d;
  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. /* NOTE: the search range was restricted to avoid too many false
  142. detections */
  143. if (p->buf_size < 6)
  144. return 0;
  145. d = p->buf;
  146. code = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3]);
  147. if ((code & 0xffffff00) == 0x100) {
  148. if (code == SEQ_START_CODE ||
  149. code == GOP_START_CODE ||
  150. code == PICTURE_START_CODE)
  151. return 50 - 1;
  152. else
  153. return 0;
  154. }
  155. return 0;
  156. }
  157. AVInputFormat mp3_iformat = {
  158. "mp3",
  159. "MPEG audio",
  160. 0,
  161. NULL,
  162. mp3_read_header,
  163. raw_read_packet,
  164. raw_read_close,
  165. .extensions = "mp2,mp3", /* XXX: use probe */
  166. };
  167. AVOutputFormat mp2_oformat = {
  168. "mp2",
  169. "MPEG audio layer 2",
  170. "audio/x-mpeg",
  171. "mp2,mp3",
  172. 0,
  173. CODEC_ID_MP2,
  174. 0,
  175. raw_write_header,
  176. raw_write_packet,
  177. raw_write_trailer,
  178. };
  179. AVInputFormat ac3_iformat = {
  180. "ac3",
  181. "raw ac3",
  182. 0,
  183. NULL,
  184. raw_read_header,
  185. raw_read_packet,
  186. raw_read_close,
  187. .extensions = "ac3",
  188. .value = CODEC_ID_AC3,
  189. };
  190. AVOutputFormat ac3_oformat = {
  191. "ac3",
  192. "raw ac3",
  193. "audio/x-ac3",
  194. "ac3",
  195. 0,
  196. CODEC_ID_AC3,
  197. 0,
  198. raw_write_header,
  199. raw_write_packet,
  200. raw_write_trailer,
  201. };
  202. AVOutputFormat h263_oformat = {
  203. "h263",
  204. "raw h263",
  205. "video/x-h263",
  206. "h263",
  207. 0,
  208. 0,
  209. CODEC_ID_H263,
  210. raw_write_header,
  211. raw_write_packet,
  212. raw_write_trailer,
  213. };
  214. AVInputFormat m4v_iformat = {
  215. "m4v",
  216. "raw MPEG4 video format",
  217. 0,
  218. NULL /*mpegvideo_probe*/,
  219. video_read_header,
  220. raw_read_packet,
  221. raw_read_close,
  222. .extensions = "m4v", //FIXME remove after writing mpeg4_probe
  223. .value = CODEC_ID_MPEG4,
  224. };
  225. AVOutputFormat m4v_oformat = {
  226. "m4v",
  227. "raw MPEG4 video format",
  228. NULL,
  229. "m4v",
  230. 0,
  231. CODEC_ID_NONE,
  232. CODEC_ID_MPEG4,
  233. raw_write_header,
  234. raw_write_packet,
  235. raw_write_trailer,
  236. };
  237. AVInputFormat mpegvideo_iformat = {
  238. "mpegvideo",
  239. "MPEG video",
  240. 0,
  241. mpegvideo_probe,
  242. video_read_header,
  243. raw_read_packet,
  244. raw_read_close,
  245. .value = CODEC_ID_MPEG1VIDEO,
  246. };
  247. AVOutputFormat mpeg1video_oformat = {
  248. "mpeg1video",
  249. "MPEG video",
  250. "video/x-mpeg",
  251. "mpg,mpeg",
  252. 0,
  253. 0,
  254. CODEC_ID_MPEG1VIDEO,
  255. raw_write_header,
  256. raw_write_packet,
  257. raw_write_trailer,
  258. };
  259. AVInputFormat mjpeg_iformat = {
  260. "mjpeg",
  261. "MJPEG video",
  262. 0,
  263. NULL,
  264. video_read_header,
  265. raw_read_packet,
  266. raw_read_close,
  267. .extensions = "mjpg,mjpeg",
  268. .value = CODEC_ID_MJPEG,
  269. };
  270. AVOutputFormat mjpeg_oformat = {
  271. "mjpeg",
  272. "MJPEG video",
  273. "video/x-mjpeg",
  274. "mjpg,mjpeg",
  275. 0,
  276. 0,
  277. CODEC_ID_MJPEG,
  278. raw_write_header,
  279. raw_write_packet,
  280. raw_write_trailer,
  281. };
  282. /* pcm formats */
  283. #define PCMDEF(name, long_name, ext, codec) \
  284. AVInputFormat pcm_ ## name ## _iformat = {\
  285. #name,\
  286. long_name,\
  287. 0,\
  288. NULL,\
  289. raw_read_header,\
  290. raw_read_packet,\
  291. raw_read_close,\
  292. .extensions = ext,\
  293. .value = codec,\
  294. };\
  295. \
  296. AVOutputFormat pcm_ ## name ## _oformat = {\
  297. #name,\
  298. long_name,\
  299. NULL,\
  300. ext,\
  301. 0,\
  302. codec,\
  303. 0,\
  304. raw_write_header,\
  305. raw_write_packet,\
  306. raw_write_trailer,\
  307. };
  308. #ifdef WORDS_BIGENDIAN
  309. #define BE_DEF(s) s
  310. #define LE_DEF(s) NULL
  311. #else
  312. #define BE_DEF(s) NULL
  313. #define LE_DEF(s) s
  314. #endif
  315. PCMDEF(s16le, "pcm signed 16 bit little endian format",
  316. LE_DEF("sw"), CODEC_ID_PCM_S16LE)
  317. PCMDEF(s16be, "pcm signed 16 bit big endian format",
  318. BE_DEF("sw"), CODEC_ID_PCM_S16BE)
  319. PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
  320. LE_DEF("uw"), CODEC_ID_PCM_U16LE)
  321. PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
  322. BE_DEF("uw"), CODEC_ID_PCM_U16BE)
  323. PCMDEF(s8, "pcm signed 8 bit format",
  324. "sb", CODEC_ID_PCM_S8)
  325. PCMDEF(u8, "pcm unsigned 8 bit format",
  326. "ub", CODEC_ID_PCM_U8)
  327. PCMDEF(mulaw, "pcm mu law format",
  328. "ul", CODEC_ID_PCM_MULAW)
  329. PCMDEF(alaw, "pcm A law format",
  330. "al", CODEC_ID_PCM_ALAW)
  331. int rawvideo_read_packet(AVFormatContext *s,
  332. AVPacket *pkt)
  333. {
  334. int packet_size, ret, width, height;
  335. AVStream *st = s->streams[0];
  336. width = st->codec.width;
  337. height = st->codec.height;
  338. switch(st->codec.pix_fmt) {
  339. case PIX_FMT_YUV420P:
  340. packet_size = (width * height * 3) / 2;
  341. break;
  342. case PIX_FMT_YUV422:
  343. packet_size = (width * height * 2);
  344. break;
  345. case PIX_FMT_BGR24:
  346. case PIX_FMT_RGB24:
  347. packet_size = (width * height * 3);
  348. break;
  349. default:
  350. av_abort();
  351. break;
  352. }
  353. if (av_new_packet(pkt, packet_size) < 0)
  354. return -EIO;
  355. pkt->stream_index = 0;
  356. #if 0
  357. /* bypass buffered I/O */
  358. ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
  359. #else
  360. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  361. #endif
  362. if (ret != pkt->size) {
  363. av_free_packet(pkt);
  364. return -EIO;
  365. } else {
  366. return 0;
  367. }
  368. }
  369. AVInputFormat rawvideo_iformat = {
  370. "rawvideo",
  371. "raw video format",
  372. 0,
  373. NULL,
  374. raw_read_header,
  375. rawvideo_read_packet,
  376. raw_read_close,
  377. .extensions = "yuv",
  378. .value = CODEC_ID_RAWVIDEO,
  379. };
  380. AVOutputFormat rawvideo_oformat = {
  381. "rawvideo",
  382. "raw video format",
  383. NULL,
  384. "yuv",
  385. 0,
  386. CODEC_ID_NONE,
  387. CODEC_ID_RAWVIDEO,
  388. raw_write_header,
  389. raw_write_packet,
  390. raw_write_trailer,
  391. };
  392. static int null_write_packet(struct AVFormatContext *s,
  393. int stream_index,
  394. unsigned char *buf, int size, int force_pts)
  395. {
  396. return 0;
  397. }
  398. AVOutputFormat null_oformat = {
  399. "null",
  400. "null video format",
  401. NULL,
  402. NULL,
  403. 0,
  404. #ifdef WORDS_BIGENDIAN
  405. CODEC_ID_PCM_S16BE,
  406. #else
  407. CODEC_ID_PCM_S16LE,
  408. #endif
  409. CODEC_ID_RAWVIDEO,
  410. raw_write_header,
  411. null_write_packet,
  412. raw_write_trailer,
  413. .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
  414. };
  415. int raw_init(void)
  416. {
  417. av_register_input_format(&mp3_iformat);
  418. av_register_output_format(&mp2_oformat);
  419. av_register_input_format(&ac3_iformat);
  420. av_register_output_format(&ac3_oformat);
  421. av_register_output_format(&h263_oformat);
  422. av_register_input_format(&m4v_iformat);
  423. av_register_output_format(&m4v_oformat);
  424. av_register_input_format(&mpegvideo_iformat);
  425. av_register_output_format(&mpeg1video_oformat);
  426. av_register_input_format(&mjpeg_iformat);
  427. av_register_output_format(&mjpeg_oformat);
  428. av_register_input_format(&pcm_s16le_iformat);
  429. av_register_output_format(&pcm_s16le_oformat);
  430. av_register_input_format(&pcm_s16be_iformat);
  431. av_register_output_format(&pcm_s16be_oformat);
  432. av_register_input_format(&pcm_u16le_iformat);
  433. av_register_output_format(&pcm_u16le_oformat);
  434. av_register_input_format(&pcm_u16be_iformat);
  435. av_register_output_format(&pcm_u16be_oformat);
  436. av_register_input_format(&pcm_s8_iformat);
  437. av_register_output_format(&pcm_s8_oformat);
  438. av_register_input_format(&pcm_u8_iformat);
  439. av_register_output_format(&pcm_u8_oformat);
  440. av_register_input_format(&pcm_mulaw_iformat);
  441. av_register_output_format(&pcm_mulaw_oformat);
  442. av_register_input_format(&pcm_alaw_iformat);
  443. av_register_output_format(&pcm_alaw_oformat);
  444. av_register_input_format(&rawvideo_iformat);
  445. av_register_output_format(&rawvideo_oformat);
  446. av_register_output_format(&null_oformat);
  447. return 0;
  448. }