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.

607 lines
14KB

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