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.

284 lines
6.0KB

  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)
  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. /* XXX: endianness */
  64. break;
  65. case CODEC_TYPE_VIDEO:
  66. st->codec.frame_rate = ap->frame_rate;
  67. st->codec.width = ap->width;
  68. st->codec.height = ap->height;
  69. break;
  70. default:
  71. abort();
  72. break;
  73. }
  74. } else {
  75. abort();
  76. }
  77. return 0;
  78. }
  79. #define MIN_SIZE 1024
  80. int raw_read_packet(AVFormatContext *s,
  81. AVPacket *pkt)
  82. {
  83. int packet_size, n, ret;
  84. if (url_feof(&s->pb))
  85. return -EIO;
  86. packet_size = url_get_packet_size(&s->pb);
  87. n = MIN_SIZE / packet_size;
  88. if (n <= 0)
  89. n = 1;
  90. if (av_new_packet(pkt, n * packet_size) < 0)
  91. return -EIO;
  92. pkt->stream_index = 0;
  93. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  94. if (ret < 0)
  95. av_free_packet(pkt);
  96. return ret;
  97. }
  98. int raw_read_close(AVFormatContext *s)
  99. {
  100. return 0;
  101. }
  102. /* mp3 read */
  103. static int mp3_read_header(AVFormatContext *s,
  104. AVFormatParameters *ap)
  105. {
  106. AVStream *st;
  107. st = malloc(sizeof(AVStream));
  108. if (!st)
  109. return -1;
  110. s->nb_streams = 1;
  111. s->streams[0] = st;
  112. st->id = 0;
  113. st->codec.codec_type = CODEC_TYPE_AUDIO;
  114. st->codec.codec_id = CODEC_ID_MP2;
  115. /* XXX: read the first frame and extract rate and channels */
  116. st->codec.sample_rate = 44100;
  117. st->codec.channels = 2;
  118. return 0;
  119. }
  120. /* mpeg1/h263 input */
  121. static int video_read_header(AVFormatContext *s,
  122. AVFormatParameters *ap)
  123. {
  124. AVStream *st;
  125. st = av_mallocz(sizeof(AVStream));
  126. if (!st)
  127. return -1;
  128. s->nb_streams = 1;
  129. s->streams[0] = st;
  130. st->codec.codec_type = CODEC_TYPE_VIDEO;
  131. st->codec.codec_id = s->format->video_codec;
  132. return 0;
  133. }
  134. AVFormat mp2_format = {
  135. "mp2",
  136. "MPEG audio",
  137. "audio/x-mpeg",
  138. "mp2,mp3",
  139. CODEC_ID_MP2,
  140. 0,
  141. raw_write_header,
  142. raw_write_packet,
  143. raw_write_trailer,
  144. mp3_read_header,
  145. raw_read_packet,
  146. raw_read_close,
  147. };
  148. AVFormat ac3_format = {
  149. "ac3",
  150. "raw ac3",
  151. "audio/x-ac3",
  152. "ac3",
  153. CODEC_ID_AC3,
  154. 0,
  155. raw_write_header,
  156. raw_write_packet,
  157. raw_write_trailer,
  158. };
  159. AVFormat h263_format = {
  160. "h263",
  161. "raw h263",
  162. "video/x-h263",
  163. "h263",
  164. 0,
  165. CODEC_ID_H263,
  166. raw_write_header,
  167. raw_write_packet,
  168. raw_write_trailer,
  169. video_read_header,
  170. raw_read_packet,
  171. raw_read_close,
  172. };
  173. AVFormat mpeg1video_format = {
  174. "mpegvideo",
  175. "MPEG video",
  176. "video/x-mpeg",
  177. "mpg,mpeg",
  178. 0,
  179. CODEC_ID_MPEG1VIDEO,
  180. raw_write_header,
  181. raw_write_packet,
  182. raw_write_trailer,
  183. video_read_header,
  184. raw_read_packet,
  185. raw_read_close,
  186. };
  187. AVFormat pcm_format = {
  188. "pcm",
  189. "pcm raw format",
  190. NULL,
  191. "sw",
  192. CODEC_ID_PCM,
  193. 0,
  194. raw_write_header,
  195. raw_write_packet,
  196. raw_write_trailer,
  197. raw_read_header,
  198. raw_read_packet,
  199. raw_read_close,
  200. };
  201. int rawvideo_read_packet(AVFormatContext *s,
  202. AVPacket *pkt)
  203. {
  204. int packet_size, ret, width, height;
  205. AVStream *st = s->streams[0];
  206. width = st->codec.width;
  207. height = st->codec.height;
  208. switch(st->codec.pix_fmt) {
  209. case PIX_FMT_YUV420P:
  210. packet_size = (width * height * 3) / 2;
  211. break;
  212. case PIX_FMT_YUV422:
  213. packet_size = (width * height * 2);
  214. break;
  215. case PIX_FMT_BGR24:
  216. case PIX_FMT_RGB24:
  217. packet_size = (width * height * 3);
  218. break;
  219. default:
  220. abort();
  221. break;
  222. }
  223. if (av_new_packet(pkt, packet_size) < 0)
  224. return -EIO;
  225. pkt->stream_index = 0;
  226. /* bypass buffered I/O */
  227. ret = url_read(url_fileno(&s->pb), pkt->data, pkt->size);
  228. if (ret != pkt->size) {
  229. av_free_packet(pkt);
  230. return -EIO;
  231. } else {
  232. return 0;
  233. }
  234. }
  235. AVFormat rawvideo_format = {
  236. "rawvideo",
  237. "raw video format",
  238. NULL,
  239. "yuv",
  240. CODEC_ID_NONE,
  241. CODEC_ID_RAWVIDEO,
  242. raw_write_header,
  243. raw_write_packet,
  244. raw_write_trailer,
  245. raw_read_header,
  246. rawvideo_read_packet,
  247. raw_read_close,
  248. };