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.

528 lines
12KB

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