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.

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