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.

274 lines
7.0KB

  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. };
  158. #define MAX_YUV4_HEADER 50
  159. #define MAX_FRAME_HEADER 10
  160. static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
  161. {
  162. char header[MAX_YUV4_HEADER+1];
  163. int i;
  164. ByteIOContext *pb = &s->pb;
  165. int width, height, raten, rated, aspectn, aspectd;
  166. char lacing;
  167. AVStream *st;
  168. for (i=0; i<MAX_YUV4_HEADER; i++) {
  169. header[i] = get_byte(pb);
  170. if (header[i] == '\n') {
  171. header[i+1] = 0;
  172. break;
  173. }
  174. }
  175. if (i == MAX_YUV4_HEADER) return -1;
  176. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
  177. sscanf(header+strlen(Y4M_MAGIC), " W%d H%d F%d:%d I%c A%d:%d",
  178. &width, &height, &raten, &rated, &lacing, &aspectn, &aspectd);
  179. st = av_new_stream(s, 0);
  180. st = s->streams[0];
  181. st->codec.width = width;
  182. st->codec.height = height;
  183. av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
  184. st->codec.frame_rate = raten;
  185. st->codec.frame_rate_base = rated;
  186. st->codec.pix_fmt = PIX_FMT_YUV420P;
  187. st->codec.codec_type = CODEC_TYPE_VIDEO;
  188. st->codec.codec_id = CODEC_ID_RAWVIDEO;
  189. return 0;
  190. }
  191. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  192. {
  193. int i;
  194. char header[MAX_FRAME_HEADER+1];
  195. int packet_size, ret, width, height;
  196. AVStream *st = s->streams[0];
  197. for (i=0; i<MAX_FRAME_HEADER; i++) {
  198. header[i] = get_byte(&s->pb);
  199. if (header[i] == '\n') {
  200. header[i+1] = 0;
  201. break;
  202. }
  203. }
  204. if (i == MAX_FRAME_HEADER) return -1;
  205. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
  206. width = st->codec.width;
  207. height = st->codec.height;
  208. packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
  209. if (packet_size < 0)
  210. av_abort();
  211. if (av_new_packet(pkt, packet_size) < 0)
  212. return -EIO;
  213. pkt->stream_index = 0;
  214. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  215. if (ret != pkt->size) {
  216. av_free_packet(pkt);
  217. return -EIO;
  218. } else {
  219. return 0;
  220. }
  221. }
  222. static int yuv4_read_close(AVFormatContext *s)
  223. {
  224. return 0;
  225. }
  226. AVInputFormat yuv4mpegpipe_iformat = {
  227. "yuv4mpegpipe",
  228. "YUV4MPEG pipe format",
  229. 0,
  230. NULL,
  231. yuv4_read_header,
  232. yuv4_read_packet,
  233. yuv4_read_close,
  234. .extensions = "yuv4mpeg"
  235. };
  236. int yuv4mpeg_init(void)
  237. {
  238. av_register_input_format(&yuv4mpegpipe_iformat);
  239. av_register_output_format(&yuv4mpegpipe_oformat);
  240. return 0;
  241. }