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.

528 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. 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. /* ac3 read */
  110. static int ac3_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_AUDIO;
  118. st->codec.codec_id = CODEC_ID_AC3;
  119. /* the parameters will be extracted from the compressed bitstream */
  120. return 0;
  121. }
  122. /* mpeg1/h263 input */
  123. static int video_read_header(AVFormatContext *s,
  124. AVFormatParameters *ap)
  125. {
  126. AVStream *st;
  127. st = av_new_stream(s, 0);
  128. if (!st)
  129. return AVERROR_NOMEM;
  130. st->codec.codec_type = CODEC_TYPE_VIDEO;
  131. st->codec.codec_id = s->iformat->value;
  132. /* for mjpeg, specify frame rate */
  133. /* for mpeg4 specify it too (most mpeg4 streams dont have the fixed_vop_rate set ...)*/
  134. if (st->codec.codec_id == CODEC_ID_MJPEG || st->codec.codec_id == CODEC_ID_MPEG4) {
  135. if (ap) {
  136. st->codec.frame_rate = ap->frame_rate;
  137. } else {
  138. st->codec.frame_rate = 25 * FRAME_RATE_BASE;
  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. int rawvideo_read_packet(AVFormatContext *s,
  344. AVPacket *pkt)
  345. {
  346. int packet_size, ret, width, height;
  347. AVStream *st = s->streams[0];
  348. width = st->codec.width;
  349. height = st->codec.height;
  350. switch(st->codec.pix_fmt) {
  351. case PIX_FMT_YUV420P:
  352. packet_size = (width * height * 3) / 2;
  353. break;
  354. case PIX_FMT_YUV422:
  355. packet_size = (width * height * 2);
  356. break;
  357. case PIX_FMT_BGR24:
  358. case PIX_FMT_RGB24:
  359. packet_size = (width * height * 3);
  360. break;
  361. default:
  362. av_abort();
  363. break;
  364. }
  365. if (av_new_packet(pkt, packet_size) < 0)
  366. return -EIO;
  367. pkt->stream_index = 0;
  368. #if 0
  369. /* bypass buffered I/O */
  370. ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
  371. #else
  372. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  373. #endif
  374. if (ret != pkt->size) {
  375. av_free_packet(pkt);
  376. return -EIO;
  377. } else {
  378. return 0;
  379. }
  380. }
  381. AVInputFormat rawvideo_iformat = {
  382. "rawvideo",
  383. "raw video format",
  384. 0,
  385. NULL,
  386. raw_read_header,
  387. rawvideo_read_packet,
  388. raw_read_close,
  389. .extensions = "yuv",
  390. .value = CODEC_ID_RAWVIDEO,
  391. };
  392. AVOutputFormat rawvideo_oformat = {
  393. "rawvideo",
  394. "raw video format",
  395. NULL,
  396. "yuv",
  397. 0,
  398. CODEC_ID_NONE,
  399. CODEC_ID_RAWVIDEO,
  400. raw_write_header,
  401. raw_write_packet,
  402. raw_write_trailer,
  403. };
  404. static int null_write_packet(struct AVFormatContext *s,
  405. int stream_index,
  406. unsigned char *buf, int size, int force_pts)
  407. {
  408. return 0;
  409. }
  410. AVOutputFormat null_oformat = {
  411. "null",
  412. "null video format",
  413. NULL,
  414. NULL,
  415. 0,
  416. #ifdef WORDS_BIGENDIAN
  417. CODEC_ID_PCM_S16BE,
  418. #else
  419. CODEC_ID_PCM_S16LE,
  420. #endif
  421. CODEC_ID_RAWVIDEO,
  422. raw_write_header,
  423. null_write_packet,
  424. raw_write_trailer,
  425. .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
  426. };
  427. int raw_init(void)
  428. {
  429. av_register_input_format(&mp3_iformat);
  430. av_register_output_format(&mp2_oformat);
  431. av_register_input_format(&ac3_iformat);
  432. av_register_output_format(&ac3_oformat);
  433. av_register_output_format(&h263_oformat);
  434. av_register_input_format(&m4v_iformat);
  435. av_register_output_format(&m4v_oformat);
  436. av_register_input_format(&mpegvideo_iformat);
  437. av_register_output_format(&mpeg1video_oformat);
  438. av_register_input_format(&mjpeg_iformat);
  439. av_register_output_format(&mjpeg_oformat);
  440. av_register_input_format(&pcm_s16le_iformat);
  441. av_register_output_format(&pcm_s16le_oformat);
  442. av_register_input_format(&pcm_s16be_iformat);
  443. av_register_output_format(&pcm_s16be_oformat);
  444. av_register_input_format(&pcm_u16le_iformat);
  445. av_register_output_format(&pcm_u16le_oformat);
  446. av_register_input_format(&pcm_u16be_iformat);
  447. av_register_output_format(&pcm_u16be_oformat);
  448. av_register_input_format(&pcm_s8_iformat);
  449. av_register_output_format(&pcm_s8_oformat);
  450. av_register_input_format(&pcm_u8_iformat);
  451. av_register_output_format(&pcm_u8_oformat);
  452. av_register_input_format(&pcm_mulaw_iformat);
  453. av_register_output_format(&pcm_mulaw_oformat);
  454. av_register_input_format(&pcm_alaw_iformat);
  455. av_register_output_format(&pcm_alaw_oformat);
  456. av_register_input_format(&rawvideo_iformat);
  457. av_register_output_format(&rawvideo_oformat);
  458. av_register_output_format(&null_oformat);
  459. return 0;
  460. }