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.

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