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.

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