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.

189 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->codecpar->width;
  35. height = st->codecpar->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. switch (st->codecpar->field_order) {
  44. case AV_FIELD_TT: inter = 't'; break;
  45. case AV_FIELD_BB: inter = 'b'; break;
  46. default: inter = 'p'; break;
  47. }
  48. switch (st->codecpar->format) {
  49. case AV_PIX_FMT_GRAY8:
  50. colorspace = " Cmono";
  51. break;
  52. case AV_PIX_FMT_YUV411P:
  53. colorspace = " C411 XYSCSS=411";
  54. break;
  55. case AV_PIX_FMT_YUV420P:
  56. switch (st->codecpar->chroma_location) {
  57. case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
  58. case AVCHROMA_LOC_LEFT: colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
  59. default: colorspace = " C420jpeg XYSCSS=420JPEG"; break;
  60. }
  61. break;
  62. case AV_PIX_FMT_YUV422P:
  63. colorspace = " C422 XYSCSS=422";
  64. break;
  65. case AV_PIX_FMT_YUV444P:
  66. colorspace = " C444 XYSCSS=444";
  67. break;
  68. }
  69. /* construct stream header, if this is the first frame */
  70. n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
  71. Y4M_MAGIC, width, height, raten, rated, inter,
  72. aspectn, aspectd, colorspace);
  73. return n;
  74. }
  75. static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
  76. {
  77. AVStream *st = s->streams[pkt->stream_index];
  78. AVIOContext *pb = s->pb;
  79. AVFrame *frame;
  80. int* first_pkt = s->priv_data;
  81. int width, height, h_chroma_shift, v_chroma_shift;
  82. int i;
  83. char buf2[Y4M_LINE_MAX + 1];
  84. char buf1[20];
  85. uint8_t *ptr, *ptr1, *ptr2;
  86. frame = (AVFrame *)pkt->data;
  87. /* for the first packet we have to output the header as well */
  88. if (*first_pkt) {
  89. *first_pkt = 0;
  90. if (yuv4_generate_header(s, buf2) < 0) {
  91. av_log(s, AV_LOG_ERROR,
  92. "Error. YUV4MPEG stream header write failed.\n");
  93. return AVERROR(EIO);
  94. } else {
  95. avio_write(pb, buf2, strlen(buf2));
  96. }
  97. }
  98. /* construct frame header */
  99. snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
  100. avio_write(pb, buf1, strlen(buf1));
  101. width = st->codecpar->width;
  102. height = st->codecpar->height;
  103. ptr = frame->data[0];
  104. for (i = 0; i < height; i++) {
  105. avio_write(pb, ptr, width);
  106. ptr += frame->linesize[0];
  107. }
  108. if (st->codecpar->format != AV_PIX_FMT_GRAY8) {
  109. // Adjust for smaller Cb and Cr planes
  110. av_pix_fmt_get_chroma_sub_sample(st->codecpar->format, &h_chroma_shift,
  111. &v_chroma_shift);
  112. // Shift right, rounding up
  113. width = AV_CEIL_RSHIFT(width, h_chroma_shift);
  114. height = AV_CEIL_RSHIFT(height, v_chroma_shift);
  115. ptr1 = frame->data[1];
  116. ptr2 = frame->data[2];
  117. for (i = 0; i < height; i++) { /* Cb */
  118. avio_write(pb, ptr1, width);
  119. ptr1 += frame->linesize[1];
  120. }
  121. for (i = 0; i < height; i++) { /* Cr */
  122. avio_write(pb, ptr2, width);
  123. ptr2 += frame->linesize[2];
  124. }
  125. }
  126. return 0;
  127. }
  128. static int yuv4_write_header(AVFormatContext *s)
  129. {
  130. int *first_pkt = s->priv_data;
  131. if (s->nb_streams != 1)
  132. return AVERROR(EIO);
  133. if (s->streams[0]->codecpar->codec_id != AV_CODEC_ID_WRAPPED_AVFRAME) {
  134. av_log(s, AV_LOG_ERROR, "ERROR: Codec not supported.\n");
  135. return AVERROR_INVALIDDATA;
  136. }
  137. if (s->streams[0]->codecpar->format == AV_PIX_FMT_YUV411P) {
  138. av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV "
  139. "stream, some mjpegtools might not work.\n");
  140. } else if ((s->streams[0]->codecpar->format != AV_PIX_FMT_YUV420P) &&
  141. (s->streams[0]->codecpar->format != AV_PIX_FMT_YUV422P) &&
  142. (s->streams[0]->codecpar->format != AV_PIX_FMT_GRAY8) &&
  143. (s->streams[0]->codecpar->format != AV_PIX_FMT_YUV444P)) {
  144. av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, "
  145. "yuv422p, yuv420p, yuv411p and gray pixel formats. "
  146. "Use -pix_fmt to select one.\n");
  147. return AVERROR(EIO);
  148. }
  149. *first_pkt = 1;
  150. return 0;
  151. }
  152. AVOutputFormat ff_yuv4mpegpipe_muxer = {
  153. .name = "yuv4mpegpipe",
  154. .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
  155. .mime_type = "",
  156. .extensions = "y4m",
  157. .priv_data_size = sizeof(int),
  158. .audio_codec = AV_CODEC_ID_NONE,
  159. .video_codec = AV_CODEC_ID_WRAPPED_AVFRAME,
  160. .write_header = yuv4_write_header,
  161. .write_packet = yuv4_write_packet,
  162. };