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.

279 lines
7.2KB

  1. /*
  2. * YUV4MPEG format
  3. * Copyright (c) 2001, 2002, 2003 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. #define Y4M_MAGIC "YUV4MPEG2"
  21. #define Y4M_FRAME_MAGIC "FRAME"
  22. #define Y4M_LINE_MAX 256
  23. #ifdef CONFIG_ENCODERS
  24. static int yuv4_write_header(AVFormatContext *s)
  25. {
  26. AVStream *st;
  27. int width, height;
  28. int raten, rated, aspectn, aspectd, n;
  29. char buf[Y4M_LINE_MAX+1];
  30. if (s->nb_streams != 1)
  31. return -EIO;
  32. st = s->streams[0];
  33. width = st->codec.width;
  34. height = st->codec.height;
  35. #if 1
  36. //this is identical to the code below for exact fps
  37. av_reduce(&raten, &rated, st->codec.frame_rate, st->codec.frame_rate_base, (1UL<<31)-1);
  38. #else
  39. {
  40. int gcd, fps, fps1;
  41. fps = st->codec.frame_rate;
  42. fps1 = (((float)fps / st->codec.frame_rate_base) * 1000);
  43. /* Sorry about this messy code, but mpeg2enc is very picky about
  44. * the framerates it accepts. */
  45. switch(fps1) {
  46. case 23976:
  47. raten = 24000; /* turn the framerate into a ratio */
  48. rated = 1001;
  49. break;
  50. case 29970:
  51. raten = 30000;
  52. rated = 1001;
  53. break;
  54. case 25000:
  55. raten = 25;
  56. rated = 1;
  57. break;
  58. case 30000:
  59. raten = 30;
  60. rated = 1;
  61. break;
  62. case 24000:
  63. raten = 24;
  64. rated = 1;
  65. break;
  66. case 50000:
  67. raten = 50;
  68. rated = 1;
  69. break;
  70. case 59940:
  71. raten = 60000;
  72. rated = 1001;
  73. break;
  74. case 60000:
  75. raten = 60;
  76. rated = 1;
  77. break;
  78. default:
  79. raten = st->codec.frame_rate; /* this setting should work, but often doesn't */
  80. rated = st->codec.frame_rate_base;
  81. gcd= av_gcd(raten, rated);
  82. raten /= gcd;
  83. rated /= gcd;
  84. break;
  85. }
  86. }
  87. #endif
  88. aspectn = 1;
  89. aspectd = 1; /* ffmpeg always uses a 1:1 aspect ratio */ //FIXME not true anymore
  90. /* construct stream header, if this is the first frame */
  91. n = snprintf(buf, sizeof(buf), "%s W%d H%d F%d:%d I%s A%d:%d\n",
  92. Y4M_MAGIC,
  93. width,
  94. height,
  95. raten, rated,
  96. "p", /* ffmpeg seems to only output progressive video */
  97. aspectn, aspectd);
  98. if (n < 0) {
  99. fprintf(stderr, "Error. YUV4MPEG stream header write failed.\n");
  100. return -EIO;
  101. } else {
  102. put_buffer(&s->pb, buf, strlen(buf));
  103. }
  104. return 0;
  105. }
  106. static int yuv4_write_packet(AVFormatContext *s, int stream_index,
  107. const uint8_t *buf, int size, int64_t pts)
  108. {
  109. AVStream *st = s->streams[stream_index];
  110. ByteIOContext *pb = &s->pb;
  111. AVPicture *picture;
  112. int width, height;
  113. int i, m;
  114. char buf1[20];
  115. uint8_t *ptr, *ptr1, *ptr2;
  116. picture = (AVPicture *)buf;
  117. /* construct frame header */
  118. m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
  119. put_buffer(pb, buf1, strlen(buf1));
  120. width = st->codec.width;
  121. height = st->codec.height;
  122. ptr = picture->data[0];
  123. for(i=0;i<height;i++) {
  124. put_buffer(pb, ptr, width);
  125. ptr += picture->linesize[0];
  126. }
  127. height >>= 1;
  128. width >>= 1;
  129. ptr1 = picture->data[1];
  130. ptr2 = picture->data[2];
  131. for(i=0;i<height;i++) { /* Cb */
  132. put_buffer(pb, ptr1, width);
  133. ptr1 += picture->linesize[1];
  134. }
  135. for(i=0;i<height;i++) { /* Cr */
  136. put_buffer(pb, ptr2, width);
  137. ptr2 += picture->linesize[2];
  138. }
  139. put_flush_packet(pb);
  140. return 0;
  141. }
  142. static int yuv4_write_trailer(AVFormatContext *s)
  143. {
  144. return 0;
  145. }
  146. AVOutputFormat yuv4mpegpipe_oformat = {
  147. "yuv4mpegpipe",
  148. "YUV4MPEG pipe format",
  149. "",
  150. "yuv4mpeg",
  151. 0,
  152. CODEC_ID_NONE,
  153. CODEC_ID_RAWVIDEO,
  154. yuv4_write_header,
  155. yuv4_write_packet,
  156. yuv4_write_trailer,
  157. .flags = AVFMT_RAWPICTURE,
  158. };
  159. #endif //CONFIG_ENCODERS
  160. /* Header size increased to allow room for optional flags */
  161. #define MAX_YUV4_HEADER 80
  162. #define MAX_FRAME_HEADER 10
  163. static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
  164. {
  165. char header[MAX_YUV4_HEADER+1];
  166. int i;
  167. ByteIOContext *pb = &s->pb;
  168. int width, height, raten, rated, aspectn, aspectd;
  169. char lacing;
  170. AVStream *st;
  171. for (i=0; i<MAX_YUV4_HEADER; i++) {
  172. header[i] = get_byte(pb);
  173. if (header[i] == '\n') {
  174. header[i+1] = 0;
  175. break;
  176. }
  177. }
  178. if (i == MAX_YUV4_HEADER) return -1;
  179. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
  180. sscanf(header+strlen(Y4M_MAGIC), " W%d H%d F%d:%d I%c A%d:%d",
  181. &width, &height, &raten, &rated, &lacing, &aspectn, &aspectd);
  182. st = av_new_stream(s, 0);
  183. st = s->streams[0];
  184. st->codec.width = width;
  185. st->codec.height = height;
  186. av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
  187. st->codec.frame_rate = raten;
  188. st->codec.frame_rate_base = rated;
  189. st->codec.pix_fmt = PIX_FMT_YUV420P;
  190. st->codec.codec_type = CODEC_TYPE_VIDEO;
  191. st->codec.codec_id = CODEC_ID_RAWVIDEO;
  192. return 0;
  193. }
  194. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  195. {
  196. int i;
  197. char header[MAX_FRAME_HEADER+1];
  198. int packet_size, ret, width, height;
  199. AVStream *st = s->streams[0];
  200. for (i=0; i<MAX_FRAME_HEADER; i++) {
  201. header[i] = get_byte(&s->pb);
  202. if (header[i] == '\n') {
  203. header[i+1] = 0;
  204. break;
  205. }
  206. }
  207. if (i == MAX_FRAME_HEADER) return -1;
  208. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
  209. width = st->codec.width;
  210. height = st->codec.height;
  211. packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
  212. if (packet_size < 0)
  213. av_abort();
  214. if (av_new_packet(pkt, packet_size) < 0)
  215. return -EIO;
  216. pkt->stream_index = 0;
  217. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  218. if (ret != pkt->size) {
  219. av_free_packet(pkt);
  220. return -EIO;
  221. } else {
  222. return 0;
  223. }
  224. }
  225. static int yuv4_read_close(AVFormatContext *s)
  226. {
  227. return 0;
  228. }
  229. AVInputFormat yuv4mpegpipe_iformat = {
  230. "yuv4mpegpipe",
  231. "YUV4MPEG pipe format",
  232. 0,
  233. NULL,
  234. yuv4_read_header,
  235. yuv4_read_packet,
  236. yuv4_read_close,
  237. .extensions = "yuv4mpeg"
  238. };
  239. int yuv4mpeg_init(void)
  240. {
  241. av_register_input_format(&yuv4mpegpipe_iformat);
  242. #ifdef CONFIG_ENCODERS
  243. av_register_output_format(&yuv4mpegpipe_oformat);
  244. #endif //CONFIG_ENCODERS
  245. return 0;
  246. }