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.

177 lines
4.7KB

  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. uint8_t *buf, int size, int force_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. };