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.

569 lines
13KB

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