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.

589 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. #ifdef CONFIG_MP3LAME
  196. "mp2",
  197. #else
  198. "mp2,mp3",
  199. #endif
  200. 0,
  201. CODEC_ID_MP2,
  202. 0,
  203. raw_write_header,
  204. raw_write_packet,
  205. raw_write_trailer,
  206. };
  207. #ifdef CONFIG_MP3LAME
  208. AVOutputFormat mp3_oformat = {
  209. "mp3",
  210. "MPEG audio layer 3",
  211. "audio/x-mpeg",
  212. "mp3",
  213. 0,
  214. CODEC_ID_MP3LAME,
  215. 0,
  216. raw_write_header,
  217. raw_write_packet,
  218. raw_write_trailer,
  219. };
  220. #endif
  221. AVInputFormat ac3_iformat = {
  222. "ac3",
  223. "raw ac3",
  224. 0,
  225. NULL,
  226. ac3_read_header,
  227. raw_read_packet,
  228. raw_read_close,
  229. .extensions = "ac3",
  230. };
  231. AVOutputFormat ac3_oformat = {
  232. "ac3",
  233. "raw ac3",
  234. "audio/x-ac3",
  235. "ac3",
  236. 0,
  237. CODEC_ID_AC3,
  238. 0,
  239. raw_write_header,
  240. raw_write_packet,
  241. raw_write_trailer,
  242. };
  243. AVOutputFormat h263_oformat = {
  244. "h263",
  245. "raw h263",
  246. "video/x-h263",
  247. "h263",
  248. 0,
  249. 0,
  250. CODEC_ID_H263,
  251. raw_write_header,
  252. raw_write_packet,
  253. raw_write_trailer,
  254. };
  255. AVInputFormat m4v_iformat = {
  256. "m4v",
  257. "raw MPEG4 video format",
  258. 0,
  259. NULL /*mpegvideo_probe*/,
  260. video_read_header,
  261. raw_read_packet,
  262. raw_read_close,
  263. .extensions = "m4v", //FIXME remove after writing mpeg4_probe
  264. .value = CODEC_ID_MPEG4,
  265. };
  266. AVOutputFormat m4v_oformat = {
  267. "m4v",
  268. "raw MPEG4 video format",
  269. NULL,
  270. "m4v",
  271. 0,
  272. CODEC_ID_NONE,
  273. CODEC_ID_MPEG4,
  274. raw_write_header,
  275. raw_write_packet,
  276. raw_write_trailer,
  277. };
  278. AVInputFormat h264_iformat = {
  279. "h264",
  280. "raw H264 video format",
  281. 0,
  282. NULL /*mpegvideo_probe*/,
  283. video_read_header,
  284. raw_read_packet,
  285. raw_read_close,
  286. .extensions = "h26l,h264", //FIXME remove after writing mpeg4_probe
  287. .value = CODEC_ID_H264,
  288. };
  289. AVOutputFormat h264_oformat = {
  290. "h264",
  291. "raw H264 video format",
  292. NULL,
  293. "h264",
  294. 0,
  295. CODEC_ID_NONE,
  296. CODEC_ID_H264,
  297. raw_write_header,
  298. raw_write_packet,
  299. raw_write_trailer,
  300. };
  301. AVInputFormat mpegvideo_iformat = {
  302. "mpegvideo",
  303. "MPEG video",
  304. 0,
  305. mpegvideo_probe,
  306. video_read_header,
  307. raw_read_packet,
  308. raw_read_close,
  309. .value = CODEC_ID_MPEG1VIDEO,
  310. };
  311. AVOutputFormat mpeg1video_oformat = {
  312. "mpeg1video",
  313. "MPEG video",
  314. "video/x-mpeg",
  315. "mpg,mpeg",
  316. 0,
  317. 0,
  318. CODEC_ID_MPEG1VIDEO,
  319. raw_write_header,
  320. raw_write_packet,
  321. raw_write_trailer,
  322. };
  323. AVInputFormat mjpeg_iformat = {
  324. "mjpeg",
  325. "MJPEG video",
  326. 0,
  327. NULL,
  328. video_read_header,
  329. raw_read_packet,
  330. raw_read_close,
  331. .extensions = "mjpg,mjpeg",
  332. .value = CODEC_ID_MJPEG,
  333. };
  334. AVOutputFormat mjpeg_oformat = {
  335. "mjpeg",
  336. "MJPEG video",
  337. "video/x-mjpeg",
  338. "mjpg,mjpeg",
  339. 0,
  340. 0,
  341. CODEC_ID_MJPEG,
  342. raw_write_header,
  343. raw_write_packet,
  344. raw_write_trailer,
  345. };
  346. /* pcm formats */
  347. #define PCMDEF(name, long_name, ext, codec) \
  348. AVInputFormat pcm_ ## name ## _iformat = {\
  349. #name,\
  350. long_name,\
  351. 0,\
  352. NULL,\
  353. raw_read_header,\
  354. raw_read_packet,\
  355. raw_read_close,\
  356. .extensions = ext,\
  357. .value = codec,\
  358. };\
  359. \
  360. AVOutputFormat pcm_ ## name ## _oformat = {\
  361. #name,\
  362. long_name,\
  363. NULL,\
  364. ext,\
  365. 0,\
  366. codec,\
  367. 0,\
  368. raw_write_header,\
  369. raw_write_packet,\
  370. raw_write_trailer,\
  371. };
  372. #ifdef WORDS_BIGENDIAN
  373. #define BE_DEF(s) s
  374. #define LE_DEF(s) NULL
  375. #else
  376. #define BE_DEF(s) NULL
  377. #define LE_DEF(s) s
  378. #endif
  379. PCMDEF(s16le, "pcm signed 16 bit little endian format",
  380. LE_DEF("sw"), CODEC_ID_PCM_S16LE)
  381. PCMDEF(s16be, "pcm signed 16 bit big endian format",
  382. BE_DEF("sw"), CODEC_ID_PCM_S16BE)
  383. PCMDEF(u16le, "pcm unsigned 16 bit little endian format",
  384. LE_DEF("uw"), CODEC_ID_PCM_U16LE)
  385. PCMDEF(u16be, "pcm unsigned 16 bit big endian format",
  386. BE_DEF("uw"), CODEC_ID_PCM_U16BE)
  387. PCMDEF(s8, "pcm signed 8 bit format",
  388. "sb", CODEC_ID_PCM_S8)
  389. PCMDEF(u8, "pcm unsigned 8 bit format",
  390. "ub", CODEC_ID_PCM_U8)
  391. PCMDEF(mulaw, "pcm mu law format",
  392. "ul", CODEC_ID_PCM_MULAW)
  393. PCMDEF(alaw, "pcm A law format",
  394. "al", CODEC_ID_PCM_ALAW)
  395. static int rawvideo_read_packet(AVFormatContext *s, AVPacket *pkt)
  396. {
  397. int packet_size, ret, width, height;
  398. AVStream *st = s->streams[0];
  399. width = st->codec.width;
  400. height = st->codec.height;
  401. switch(st->codec.pix_fmt) {
  402. case PIX_FMT_YUV420P:
  403. packet_size = (width * height * 3) / 2;
  404. break;
  405. case PIX_FMT_YUV422:
  406. packet_size = (width * height * 2);
  407. break;
  408. case PIX_FMT_BGR24:
  409. case PIX_FMT_RGB24:
  410. packet_size = (width * height * 3);
  411. break;
  412. default:
  413. av_abort();
  414. break;
  415. }
  416. if (av_new_packet(pkt, packet_size) < 0)
  417. return -EIO;
  418. pkt->stream_index = 0;
  419. #if 0
  420. /* bypass buffered I/O */
  421. ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
  422. #else
  423. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  424. #endif
  425. if (ret != pkt->size) {
  426. av_free_packet(pkt);
  427. return -EIO;
  428. } else {
  429. return 0;
  430. }
  431. }
  432. AVInputFormat rawvideo_iformat = {
  433. "rawvideo",
  434. "raw video format",
  435. 0,
  436. NULL,
  437. raw_read_header,
  438. rawvideo_read_packet,
  439. raw_read_close,
  440. .extensions = "yuv",
  441. .value = CODEC_ID_RAWVIDEO,
  442. };
  443. AVOutputFormat rawvideo_oformat = {
  444. "rawvideo",
  445. "raw video format",
  446. NULL,
  447. "yuv",
  448. 0,
  449. CODEC_ID_NONE,
  450. CODEC_ID_RAWVIDEO,
  451. raw_write_header,
  452. raw_write_packet,
  453. raw_write_trailer,
  454. };
  455. static int null_write_packet(struct AVFormatContext *s,
  456. int stream_index,
  457. unsigned char *buf, int size, int force_pts)
  458. {
  459. return 0;
  460. }
  461. AVOutputFormat null_oformat = {
  462. "null",
  463. "null video format",
  464. NULL,
  465. NULL,
  466. 0,
  467. #ifdef WORDS_BIGENDIAN
  468. CODEC_ID_PCM_S16BE,
  469. #else
  470. CODEC_ID_PCM_S16LE,
  471. #endif
  472. CODEC_ID_RAWVIDEO,
  473. raw_write_header,
  474. null_write_packet,
  475. raw_write_trailer,
  476. .flags = AVFMT_NOFILE | AVFMT_RAWPICTURE,
  477. };
  478. int raw_init(void)
  479. {
  480. av_register_input_format(&mp3_iformat);
  481. av_register_output_format(&mp2_oformat);
  482. #ifdef CONFIG_MP3LAME
  483. av_register_output_format(&mp3_oformat);
  484. #endif
  485. av_register_input_format(&ac3_iformat);
  486. av_register_output_format(&ac3_oformat);
  487. av_register_output_format(&h263_oformat);
  488. av_register_input_format(&m4v_iformat);
  489. av_register_output_format(&m4v_oformat);
  490. av_register_input_format(&h264_iformat);
  491. av_register_output_format(&h264_oformat);
  492. av_register_input_format(&mpegvideo_iformat);
  493. av_register_output_format(&mpeg1video_oformat);
  494. av_register_input_format(&mjpeg_iformat);
  495. av_register_output_format(&mjpeg_oformat);
  496. av_register_input_format(&pcm_s16le_iformat);
  497. av_register_output_format(&pcm_s16le_oformat);
  498. av_register_input_format(&pcm_s16be_iformat);
  499. av_register_output_format(&pcm_s16be_oformat);
  500. av_register_input_format(&pcm_u16le_iformat);
  501. av_register_output_format(&pcm_u16le_oformat);
  502. av_register_input_format(&pcm_u16be_iformat);
  503. av_register_output_format(&pcm_u16be_oformat);
  504. av_register_input_format(&pcm_s8_iformat);
  505. av_register_output_format(&pcm_s8_oformat);
  506. av_register_input_format(&pcm_u8_iformat);
  507. av_register_output_format(&pcm_u8_oformat);
  508. av_register_input_format(&pcm_mulaw_iformat);
  509. av_register_output_format(&pcm_mulaw_oformat);
  510. av_register_input_format(&pcm_alaw_iformat);
  511. av_register_output_format(&pcm_alaw_oformat);
  512. av_register_input_format(&rawvideo_iformat);
  513. av_register_output_format(&rawvideo_oformat);
  514. av_register_output_format(&null_oformat);
  515. return 0;
  516. }