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.

257 lines
6.8KB

  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_generate_header(AVFormatContext *s, char* buf)
  25. {
  26. AVStream *st;
  27. int width, height;
  28. int raten, rated, aspectn, aspectd, n;
  29. char inter;
  30. st = s->streams[0];
  31. width = st->codec.width;
  32. height = st->codec.height;
  33. av_reduce(&raten, &rated, st->codec.frame_rate, st->codec.frame_rate_base, (1UL<<31)-1);
  34. aspectn = st->codec.sample_aspect_ratio.num;
  35. aspectd = st->codec.sample_aspect_ratio.den;
  36. inter = 'p'; /* progressive is the default */
  37. if (st->codec.coded_frame && st->codec.coded_frame->interlaced_frame) {
  38. inter = st->codec.coded_frame->top_field_first ? 't' : 'b';
  39. }
  40. /* construct stream header, if this is the first frame */
  41. n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
  42. Y4M_MAGIC,
  43. width,
  44. height,
  45. raten, rated,
  46. inter,
  47. aspectn, aspectd,
  48. (st->codec.pix_fmt == PIX_FMT_YUV411P) ? " C411 XYSCSS=411" : " C420mpeg2 XYSCSS=420MPEG2");
  49. return n;
  50. }
  51. static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
  52. {
  53. AVStream *st = s->streams[pkt->stream_index];
  54. ByteIOContext *pb = &s->pb;
  55. AVPicture *picture;
  56. int* first_pkt = s->priv_data;
  57. int width, height;
  58. int i, m;
  59. char buf2[Y4M_LINE_MAX+1];
  60. char buf1[20];
  61. uint8_t *ptr, *ptr1, *ptr2;
  62. picture = (AVPicture *)pkt->data;
  63. /* for the first packet we have to output the header as well */
  64. if (*first_pkt) {
  65. *first_pkt = 0;
  66. if (yuv4_generate_header(s, buf2) < 0) {
  67. av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
  68. return -EIO;
  69. } else {
  70. put_buffer(pb, buf2, strlen(buf2));
  71. }
  72. }
  73. /* construct frame header */
  74. m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
  75. put_buffer(pb, buf1, strlen(buf1));
  76. width = st->codec.width;
  77. height = st->codec.height;
  78. ptr = picture->data[0];
  79. for(i=0;i<height;i++) {
  80. put_buffer(pb, ptr, width);
  81. ptr += picture->linesize[0];
  82. }
  83. height >>= 1;
  84. width >>= 1;
  85. ptr1 = picture->data[1];
  86. ptr2 = picture->data[2];
  87. for(i=0;i<height;i++) { /* Cb */
  88. put_buffer(pb, ptr1, width);
  89. ptr1 += picture->linesize[1];
  90. }
  91. for(i=0;i<height;i++) { /* Cr */
  92. put_buffer(pb, ptr2, width);
  93. ptr2 += picture->linesize[2];
  94. }
  95. put_flush_packet(pb);
  96. return 0;
  97. }
  98. static int yuv4_write_header(AVFormatContext *s)
  99. {
  100. int* first_pkt = s->priv_data;
  101. if (s->nb_streams != 1)
  102. return -EIO;
  103. if (s->streams[0]->codec.pix_fmt == PIX_FMT_YUV411P) {
  104. av_log(s, AV_LOG_ERROR, "Warning: generating non-standard 4:1:1 YUV stream, some mjpegtools might not work.\n");
  105. }
  106. else if (s->streams[0]->codec.pix_fmt != PIX_FMT_YUV420P) {
  107. av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles 4:2:0, 4:1:1 YUV data. Use -pix_fmt to select one.\n");
  108. return -EIO;
  109. }
  110. *first_pkt = 1;
  111. return 0;
  112. }
  113. static int yuv4_write_trailer(AVFormatContext *s)
  114. {
  115. return 0;
  116. }
  117. AVOutputFormat yuv4mpegpipe_oformat = {
  118. "yuv4mpegpipe",
  119. "YUV4MPEG pipe format",
  120. "",
  121. "yuv4mpeg",
  122. sizeof(int),
  123. CODEC_ID_NONE,
  124. CODEC_ID_RAWVIDEO,
  125. yuv4_write_header,
  126. yuv4_write_packet,
  127. yuv4_write_trailer,
  128. .flags = AVFMT_RAWPICTURE,
  129. };
  130. #endif //CONFIG_ENCODERS
  131. /* Header size increased to allow room for optional flags */
  132. #define MAX_YUV4_HEADER 80
  133. #define MAX_FRAME_HEADER 10
  134. static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
  135. {
  136. char header[MAX_YUV4_HEADER+1];
  137. int i;
  138. ByteIOContext *pb = &s->pb;
  139. int width, height, raten, rated, aspectn, aspectd;
  140. char lacing;
  141. AVStream *st;
  142. for (i=0; i<MAX_YUV4_HEADER; i++) {
  143. header[i] = get_byte(pb);
  144. if (header[i] == '\n') {
  145. header[i+1] = 0;
  146. break;
  147. }
  148. }
  149. if (i == MAX_YUV4_HEADER) return -1;
  150. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
  151. sscanf(header+strlen(Y4M_MAGIC), " W%d H%d F%d:%d I%c A%d:%d",
  152. &width, &height, &raten, &rated, &lacing, &aspectn, &aspectd);
  153. st = av_new_stream(s, 0);
  154. st = s->streams[0];
  155. st->codec.width = width;
  156. st->codec.height = height;
  157. av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
  158. st->codec.frame_rate = raten;
  159. st->codec.frame_rate_base = rated;
  160. st->codec.pix_fmt = PIX_FMT_YUV420P;
  161. st->codec.codec_type = CODEC_TYPE_VIDEO;
  162. st->codec.codec_id = CODEC_ID_RAWVIDEO;
  163. st->codec.sample_aspect_ratio= (AVRational){aspectn, aspectd};
  164. return 0;
  165. }
  166. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  167. {
  168. int i;
  169. char header[MAX_FRAME_HEADER+1];
  170. int packet_size, ret, width, height;
  171. AVStream *st = s->streams[0];
  172. for (i=0; i<MAX_FRAME_HEADER; i++) {
  173. header[i] = get_byte(&s->pb);
  174. if (header[i] == '\n') {
  175. header[i+1] = 0;
  176. break;
  177. }
  178. }
  179. if (i == MAX_FRAME_HEADER) return -1;
  180. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
  181. width = st->codec.width;
  182. height = st->codec.height;
  183. packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
  184. if (packet_size < 0)
  185. av_abort();
  186. if (av_new_packet(pkt, packet_size) < 0)
  187. return -EIO;
  188. pkt->stream_index = 0;
  189. ret = get_buffer(&s->pb, pkt->data, pkt->size);
  190. if (ret != pkt->size) {
  191. av_free_packet(pkt);
  192. return -EIO;
  193. } else {
  194. return 0;
  195. }
  196. }
  197. static int yuv4_read_close(AVFormatContext *s)
  198. {
  199. return 0;
  200. }
  201. AVInputFormat yuv4mpegpipe_iformat = {
  202. "yuv4mpegpipe",
  203. "YUV4MPEG pipe format",
  204. 0,
  205. NULL,
  206. yuv4_read_header,
  207. yuv4_read_packet,
  208. yuv4_read_close,
  209. .extensions = "yuv4mpeg"
  210. };
  211. int yuv4mpeg_init(void)
  212. {
  213. av_register_input_format(&yuv4mpegpipe_iformat);
  214. #ifdef CONFIG_ENCODERS
  215. av_register_output_format(&yuv4mpegpipe_oformat);
  216. #endif //CONFIG_ENCODERS
  217. return 0;
  218. }