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.

275 lines
7.1KB

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