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.

289 lines
6.1KB

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