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.

188 lines
6.0KB

  1. /*
  2. * YUV4MPEG muxer
  3. * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/pixdesc.h"
  22. #include "avformat.h"
  23. #include "internal.h"
  24. #include "yuv4mpeg.h"
  25. #define Y4M_LINE_MAX 256
  26. static int yuv4_generate_header(AVFormatContext *s, char* buf)
  27. {
  28. AVStream *st;
  29. int width, height;
  30. int raten, rated, aspectn, aspectd, n;
  31. char inter;
  32. const char *colorspace = "";
  33. st = s->streams[0];
  34. width = st->codec->width;
  35. height = st->codec->height;
  36. // TODO: should be avg_frame_rate
  37. av_reduce(&raten, &rated, st->time_base.den,
  38. st->time_base.num, (1UL << 31) - 1);
  39. aspectn = st->sample_aspect_ratio.num;
  40. aspectd = st->sample_aspect_ratio.den;
  41. if (aspectn == 0 && aspectd == 1)
  42. aspectd = 0; // 0:0 means unknown
  43. inter = 'p'; /* progressive is the default */
  44. if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame)
  45. inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
  46. switch (st->codec->pix_fmt) {
  47. case AV_PIX_FMT_GRAY8:
  48. colorspace = " Cmono";
  49. break;
  50. case AV_PIX_FMT_YUV411P:
  51. colorspace = " C411 XYSCSS=411";
  52. break;
  53. case AV_PIX_FMT_YUV420P:
  54. switch (st->codec->chroma_sample_location) {
  55. case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
  56. case AVCHROMA_LOC_LEFT: colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
  57. default: colorspace = " C420jpeg XYSCSS=420JPEG"; break;
  58. }
  59. break;
  60. case AV_PIX_FMT_YUV422P:
  61. colorspace = " C422 XYSCSS=422";
  62. break;
  63. case AV_PIX_FMT_YUV444P:
  64. colorspace = " C444 XYSCSS=444";
  65. break;
  66. }
  67. /* construct stream header, if this is the first frame */
  68. n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
  69. Y4M_MAGIC, width, height, raten, rated, inter,
  70. aspectn, aspectd, colorspace);
  71. return n;
  72. }
  73. static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
  74. {
  75. AVStream *st = s->streams[pkt->stream_index];
  76. AVIOContext *pb = s->pb;
  77. AVPicture *picture;
  78. int* first_pkt = s->priv_data;
  79. int width, height, h_chroma_shift, v_chroma_shift;
  80. int i;
  81. char buf2[Y4M_LINE_MAX + 1];
  82. char buf1[20];
  83. uint8_t *ptr, *ptr1, *ptr2;
  84. picture = (AVPicture *)pkt->data;
  85. /* for the first packet we have to output the header as well */
  86. if (*first_pkt) {
  87. *first_pkt = 0;
  88. if (yuv4_generate_header(s, buf2) < 0) {
  89. av_log(s, AV_LOG_ERROR,
  90. "Error. YUV4MPEG stream header write failed.\n");
  91. return AVERROR(EIO);
  92. } else {
  93. avio_write(pb, buf2, strlen(buf2));
  94. }
  95. }
  96. /* construct frame header */
  97. snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
  98. avio_write(pb, buf1, strlen(buf1));
  99. width = st->codec->width;
  100. height = st->codec->height;
  101. ptr = picture->data[0];
  102. for (i = 0; i < height; i++) {
  103. avio_write(pb, ptr, width);
  104. ptr += picture->linesize[0];
  105. }
  106. if (st->codec->pix_fmt != AV_PIX_FMT_GRAY8) {
  107. // Adjust for smaller Cb and Cr planes
  108. av_pix_fmt_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift,
  109. &v_chroma_shift);
  110. // Shift right, rounding up
  111. width = -(-width >> h_chroma_shift);
  112. height = -(-height >> v_chroma_shift);
  113. ptr1 = picture->data[1];
  114. ptr2 = picture->data[2];
  115. for (i = 0; i < height; i++) { /* Cb */
  116. avio_write(pb, ptr1, width);
  117. ptr1 += picture->linesize[1];
  118. }
  119. for (i = 0; i < height; i++) { /* Cr */
  120. avio_write(pb, ptr2, width);
  121. ptr2 += picture->linesize[2];
  122. }
  123. }
  124. return 0;
  125. }
  126. static int yuv4_write_header(AVFormatContext *s)
  127. {
  128. int *first_pkt = s->priv_data;
  129. if (s->nb_streams != 1)
  130. return AVERROR(EIO);
  131. if (s->streams[0]->codec->codec_id != AV_CODEC_ID_RAWVIDEO) {
  132. av_log(s, AV_LOG_ERROR, "ERROR: Only rawvideo supported.\n");
  133. return AVERROR_INVALIDDATA;
  134. }
  135. if (s->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUV411P) {
  136. av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV "
  137. "stream, some mjpegtools might not work.\n");
  138. } else if ((s->streams[0]->codec->pix_fmt != AV_PIX_FMT_YUV420P) &&
  139. (s->streams[0]->codec->pix_fmt != AV_PIX_FMT_YUV422P) &&
  140. (s->streams[0]->codec->pix_fmt != AV_PIX_FMT_GRAY8) &&
  141. (s->streams[0]->codec->pix_fmt != AV_PIX_FMT_YUV444P)) {
  142. av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, "
  143. "yuv422p, yuv420p, yuv411p and gray pixel formats. "
  144. "Use -pix_fmt to select one.\n");
  145. return AVERROR(EIO);
  146. }
  147. *first_pkt = 1;
  148. return 0;
  149. }
  150. AVOutputFormat ff_yuv4mpegpipe_muxer = {
  151. .name = "yuv4mpegpipe",
  152. .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
  153. .mime_type = "",
  154. .extensions = "y4m",
  155. .priv_data_size = sizeof(int),
  156. .audio_codec = AV_CODEC_ID_NONE,
  157. .video_codec = AV_CODEC_ID_RAWVIDEO,
  158. .write_header = yuv4_write_header,
  159. .write_packet = yuv4_write_packet,
  160. .flags = AVFMT_RAWPICTURE,
  161. };