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.

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