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.

454 lines
8.9KB

  1. /*
  2. * RAW encoder and decoder
  3. * Copyright (c) 2001 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include "avformat.h"
  20. /* simple formats */
  21. int raw_write_header(struct AVFormatContext *s)
  22. {
  23. return 0;
  24. }
  25. int raw_write_packet(struct AVFormatContext *s,
  26. int stream_index,
  27. unsigned char *buf, int size, int force_pts)
  28. {
  29. put_buffer(&s->pb, buf, size);
  30. put_flush_packet(&s->pb);
  31. return 0;
  32. }
  33. int raw_write_trailer(struct AVFormatContext *s)
  34. {
  35. return 0;
  36. }
  37. /* raw input */
  38. static int raw_read_header(AVFormatContext *s,
  39. AVFormatParameters *ap)
  40. {
  41. AVStream *st;
  42. st = malloc(sizeof(AVStream));
  43. if (!st)
  44. return -1;
  45. s->nb_streams = 1;
  46. s->streams[0] = st;
  47. st->id = 0;
  48. if (ap) {
  49. if (s->format->audio_codec != CODEC_ID_NONE) {
  50. st->codec.codec_type = CODEC_TYPE_AUDIO;
  51. st->codec.codec_id = s->format->audio_codec;
  52. } else if (s->format->video_codec != CODEC_ID_NONE) {
  53. st->codec.codec_type = CODEC_TYPE_VIDEO;
  54. st->codec.codec_id = s->format->video_codec;
  55. } else {
  56. free(st);
  57. return -1;
  58. }
  59. switch(st->codec.codec_type) {
  60. case CODEC_TYPE_AUDIO:
  61. st->codec.sample_rate = ap->sample_rate;
  62. st->codec.channels = ap->channels;
  63. break;
  64. case CODEC_TYPE_VIDEO:
  65. st->codec.frame_rate = ap->frame_rate;
  66. st->codec.width = ap->width;
  67. st->codec.height = ap->height;
  68. break;
  69. default:
  70. return -1;
  71. }
  72. } else {
  73. return -1;
  74. }
  75. return 0;
  76. }
  77. /* raw input */
  78. static int pcm_read_header(AVFormatContext *s,
  79. AVFormatParameters *ap)
  80. {
  81. AVStream *st;
  82. st = malloc(sizeof(AVStream));
  83. if (!st)
  84. return -1;
  85. s->nb_streams = 1;
  86. s->streams[0] = st;
  87. st->id = 0;
  88. st->codec.codec_type = CODEC_TYPE_AUDIO;
  89. st->codec.codec_id = s->format->audio_codec;
  90. return 0;
  91. }
  92. #define RAW_PACKET_SIZE 1024
  93. int raw_read_packet(AVFormatContext *s,
  94. AVPacket *pkt)
  95. {
  96. int ret;
  97. if (av_new_packet(pkt, RAW_PACKET_SIZE) < 0)
  98. return -EIO;
  99. pkt->stream_index = 0;
  100. ret = get_buffer(&s->pb, pkt->data, RAW_PACKET_SIZE);
  101. if (ret <= 0) {
  102. av_free_packet(pkt);
  103. return -EIO;
  104. }
  105. /* note: we need to modify the packet size here to handle the last
  106. packet */
  107. pkt->size = ret;
  108. return ret;
  109. }
  110. int raw_read_close(AVFormatContext *s)
  111. {
  112. return 0;
  113. }
  114. /* mp3 read */
  115. static int mp3_read_header(AVFormatContext *s,
  116. AVFormatParameters *ap)
  117. {
  118. AVStream *st;
  119. st = malloc(sizeof(AVStream));
  120. if (!st)
  121. return -1;
  122. s->nb_streams = 1;
  123. s->streams[0] = st;
  124. st->id = 0;
  125. st->codec.codec_type = CODEC_TYPE_AUDIO;
  126. st->codec.codec_id = CODEC_ID_MP2;
  127. /* the parameters will be extracted from the compressed bitstream */
  128. return 0;
  129. }
  130. /* mpeg1/h263 input */
  131. static int video_read_header(AVFormatContext *s,
  132. AVFormatParameters *ap)
  133. {
  134. AVStream *st;
  135. st = av_mallocz(sizeof(AVStream));
  136. if (!st)
  137. return -1;
  138. s->nb_streams = 1;
  139. s->streams[0] = st;
  140. st->codec.codec_type = CODEC_TYPE_VIDEO;
  141. st->codec.codec_id = s->format->video_codec;
  142. /* for mjpeg, specify frame rate */
  143. if (st->codec.codec_id == CODEC_ID_MJPEG) {
  144. if (ap) {
  145. st->codec.frame_rate = ap->frame_rate;
  146. } else {
  147. st->codec.frame_rate = 25 * FRAME_RATE_BASE;
  148. }
  149. }
  150. return 0;
  151. }
  152. AVFormat mp2_format = {
  153. "mp2",
  154. "MPEG audio",
  155. "audio/x-mpeg",
  156. "mp2,mp3",
  157. CODEC_ID_MP2,
  158. 0,
  159. raw_write_header,
  160. raw_write_packet,
  161. raw_write_trailer,
  162. mp3_read_header,
  163. raw_read_packet,
  164. raw_read_close,
  165. };
  166. AVFormat ac3_format = {
  167. "ac3",
  168. "raw ac3",
  169. "audio/x-ac3",
  170. "ac3",
  171. CODEC_ID_AC3,
  172. 0,
  173. raw_write_header,
  174. raw_write_packet,
  175. raw_write_trailer,
  176. raw_read_header,
  177. raw_read_packet,
  178. raw_read_close,
  179. };
  180. AVFormat h263_format = {
  181. "h263",
  182. "raw h263",
  183. "video/x-h263",
  184. "h263",
  185. 0,
  186. CODEC_ID_H263,
  187. raw_write_header,
  188. raw_write_packet,
  189. raw_write_trailer,
  190. video_read_header,
  191. raw_read_packet,
  192. raw_read_close,
  193. };
  194. AVFormat mpeg1video_format = {
  195. "mpegvideo",
  196. "MPEG video",
  197. "video/x-mpeg",
  198. "mpg,mpeg",
  199. 0,
  200. CODEC_ID_MPEG1VIDEO,
  201. raw_write_header,
  202. raw_write_packet,
  203. raw_write_trailer,
  204. video_read_header,
  205. raw_read_packet,
  206. raw_read_close,
  207. };
  208. AVFormat mjpeg_format = {
  209. "mjpeg",
  210. "MJPEG video",
  211. "video/x-mjpeg",
  212. "mjpg,mjpeg",
  213. 0,
  214. CODEC_ID_MJPEG,
  215. raw_write_header,
  216. raw_write_packet,
  217. raw_write_trailer,
  218. video_read_header,
  219. raw_read_packet,
  220. raw_read_close,
  221. };
  222. /* pcm formats */
  223. AVFormat pcm_s16le_format = {
  224. "s16le",
  225. "pcm signed 16 bit little endian format",
  226. NULL,
  227. #ifdef WORDS_BIGENDIAN
  228. "",
  229. #else
  230. "sw",
  231. #endif
  232. CODEC_ID_PCM_S16LE,
  233. 0,
  234. raw_write_header,
  235. raw_write_packet,
  236. raw_write_trailer,
  237. pcm_read_header,
  238. raw_read_packet,
  239. raw_read_close,
  240. };
  241. AVFormat pcm_s16be_format = {
  242. "s16be",
  243. "pcm signed 16 bit big endian format",
  244. NULL,
  245. #ifdef WORDS_BIGENDIAN
  246. "sw",
  247. #else
  248. "",
  249. #endif
  250. CODEC_ID_PCM_S16BE,
  251. 0,
  252. raw_write_header,
  253. raw_write_packet,
  254. raw_write_trailer,
  255. pcm_read_header,
  256. raw_read_packet,
  257. raw_read_close,
  258. };
  259. AVFormat pcm_u16le_format = {
  260. "u16le",
  261. "pcm unsigned 16 bit little endian format",
  262. NULL,
  263. #ifdef WORDS_BIGENDIAN
  264. "",
  265. #else
  266. "uw",
  267. #endif
  268. CODEC_ID_PCM_U16LE,
  269. 0,
  270. raw_write_header,
  271. raw_write_packet,
  272. raw_write_trailer,
  273. pcm_read_header,
  274. raw_read_packet,
  275. raw_read_close,
  276. };
  277. AVFormat pcm_u16be_format = {
  278. "u16be",
  279. "pcm unsigned 16 bit big endian format",
  280. NULL,
  281. #ifdef WORDS_BIGENDIAN
  282. "uw",
  283. #else
  284. "",
  285. #endif
  286. CODEC_ID_PCM_U16BE,
  287. 0,
  288. raw_write_header,
  289. raw_write_packet,
  290. raw_write_trailer,
  291. pcm_read_header,
  292. raw_read_packet,
  293. raw_read_close,
  294. };
  295. AVFormat pcm_s8_format = {
  296. "s8",
  297. "pcm signed 8 bit format",
  298. NULL,
  299. "sb",
  300. CODEC_ID_PCM_S8,
  301. 0,
  302. raw_write_header,
  303. raw_write_packet,
  304. raw_write_trailer,
  305. pcm_read_header,
  306. raw_read_packet,
  307. raw_read_close,
  308. };
  309. AVFormat pcm_u8_format = {
  310. "u8",
  311. "pcm unsigned 8 bit format",
  312. NULL,
  313. "ub",
  314. CODEC_ID_PCM_U8,
  315. 0,
  316. raw_write_header,
  317. raw_write_packet,
  318. raw_write_trailer,
  319. pcm_read_header,
  320. raw_read_packet,
  321. raw_read_close,
  322. };
  323. AVFormat pcm_mulaw_format = {
  324. "mulaw",
  325. "pcm mu law format",
  326. NULL,
  327. "ul",
  328. CODEC_ID_PCM_MULAW,
  329. 0,
  330. raw_write_header,
  331. raw_write_packet,
  332. raw_write_trailer,
  333. pcm_read_header,
  334. raw_read_packet,
  335. raw_read_close,
  336. };
  337. AVFormat pcm_alaw_format = {
  338. "alaw",
  339. "pcm A law format",
  340. NULL,
  341. "al",
  342. CODEC_ID_PCM_ALAW,
  343. 0,
  344. raw_write_header,
  345. raw_write_packet,
  346. raw_write_trailer,
  347. pcm_read_header,
  348. raw_read_packet,
  349. raw_read_close,
  350. };
  351. int rawvideo_read_packet(AVFormatContext *s,
  352. AVPacket *pkt)
  353. {
  354. int packet_size, ret, width, height;
  355. AVStream *st = s->streams[0];
  356. width = st->codec.width;
  357. height = st->codec.height;
  358. switch(st->codec.pix_fmt) {
  359. case PIX_FMT_YUV420P:
  360. packet_size = (width * height * 3) / 2;
  361. break;
  362. case PIX_FMT_YUV422:
  363. packet_size = (width * height * 2);
  364. break;
  365. case PIX_FMT_BGR24:
  366. case PIX_FMT_RGB24:
  367. packet_size = (width * height * 3);
  368. break;
  369. default:
  370. abort();
  371. break;
  372. }
  373. if (av_new_packet(pkt, packet_size) < 0)
  374. return -EIO;
  375. pkt->stream_index = 0;
  376. /* bypass buffered I/O */
  377. ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
  378. if (ret != pkt->size) {
  379. av_free_packet(pkt);
  380. return -EIO;
  381. } else {
  382. return 0;
  383. }
  384. }
  385. AVFormat rawvideo_format = {
  386. "rawvideo",
  387. "raw video format",
  388. NULL,
  389. "yuv",
  390. CODEC_ID_NONE,
  391. CODEC_ID_RAWVIDEO,
  392. raw_write_header,
  393. raw_write_packet,
  394. raw_write_trailer,
  395. raw_read_header,
  396. rawvideo_read_packet,
  397. raw_read_close,
  398. };