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.

173 lines
4.5KB

  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, fps, fps1, n, gcd;
  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. fps = st->codec.frame_rate;
  39. fps1 = (((float)fps / st->codec.frame_rate_base) * 1000);
  40. /* Sorry about this messy code, but mpeg2enc is very picky about
  41. * the framerates it accepts. */
  42. switch(fps1) {
  43. case 23976:
  44. raten = 24000; /* turn the framerate into a ratio */
  45. rated = 1001;
  46. break;
  47. case 29970:
  48. raten = 30000;
  49. rated = 1001;
  50. break;
  51. case 25000:
  52. raten = 25;
  53. rated = 1;
  54. break;
  55. case 30000:
  56. raten = 30;
  57. rated = 1;
  58. break;
  59. case 24000:
  60. raten = 24;
  61. rated = 1;
  62. break;
  63. case 50000:
  64. raten = 50;
  65. rated = 1;
  66. break;
  67. case 59940:
  68. raten = 60000;
  69. rated = 1001;
  70. break;
  71. case 60000:
  72. raten = 60;
  73. rated = 1;
  74. break;
  75. default:
  76. raten = st->codec.frame_rate; /* this setting should work, but often doesn't */
  77. rated = st->codec.frame_rate_base;
  78. gcd= av_gcd(raten, rated);
  79. raten /= gcd;
  80. rated /= gcd;
  81. break;
  82. }
  83. #endif
  84. aspectn = 1;
  85. aspectd = 1; /* ffmpeg always uses a 1:1 aspect ratio */ //FIXME not true anymore
  86. /* construct stream header, if this is the first frame */
  87. n = snprintf(buf, sizeof(buf), "%s W%d H%d F%d:%d I%s A%d:%d\n",
  88. Y4M_MAGIC,
  89. width,
  90. height,
  91. raten, rated,
  92. "p", /* ffmpeg seems to only output progressive video */
  93. aspectn, aspectd);
  94. if (n < 0) {
  95. fprintf(stderr, "Error. YUV4MPEG stream header write failed.\n");
  96. return -EIO;
  97. } else {
  98. put_buffer(&s->pb, buf, strlen(buf));
  99. }
  100. return 0;
  101. }
  102. static int yuv4_write_packet(AVFormatContext *s, int stream_index,
  103. uint8_t *buf, int size, int force_pts)
  104. {
  105. AVStream *st = s->streams[stream_index];
  106. ByteIOContext *pb = &s->pb;
  107. AVPicture *picture;
  108. int width, height;
  109. int i, m;
  110. char buf1[20];
  111. uint8_t *ptr, *ptr1, *ptr2;
  112. picture = (AVPicture *)buf;
  113. /* construct frame header */
  114. m = snprintf(buf1, sizeof(buf1), "%s \n", Y4M_FRAME_MAGIC);
  115. put_buffer(pb, buf1, strlen(buf1));
  116. width = st->codec.width;
  117. height = st->codec.height;
  118. ptr = picture->data[0];
  119. for(i=0;i<height;i++) {
  120. put_buffer(pb, ptr, width);
  121. ptr += picture->linesize[0];
  122. }
  123. height >>= 1;
  124. width >>= 1;
  125. ptr1 = picture->data[1];
  126. ptr2 = picture->data[2];
  127. for(i=0;i<height;i++) { /* Cb */
  128. put_buffer(pb, ptr1, width);
  129. ptr1 += picture->linesize[1];
  130. }
  131. for(i=0;i<height;i++) { /* Cr */
  132. put_buffer(pb, ptr2, width);
  133. ptr2 += picture->linesize[2];
  134. }
  135. put_flush_packet(pb);
  136. return 0;
  137. }
  138. static int yuv4_write_trailer(AVFormatContext *s)
  139. {
  140. return 0;
  141. }
  142. AVOutputFormat yuv4mpegpipe_oformat = {
  143. "yuv4mpegpipe",
  144. "YUV4MPEG pipe format",
  145. "",
  146. "yuv4mpeg",
  147. 0,
  148. CODEC_ID_NONE,
  149. CODEC_ID_RAWVIDEO,
  150. yuv4_write_header,
  151. yuv4_write_packet,
  152. yuv4_write_trailer,
  153. .flags = AVFMT_RAWPICTURE,
  154. };