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.

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