LCOV - code coverage report
Current view: top level - libavformat - yuv4mpegenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 65 166 39.2 %
Date: 2017-12-16 21:16:39 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * YUV4MPEG muxer
       3             :  * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg 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             :  * FFmpeg 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 FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include "libavutil/pixdesc.h"
      23             : #include "avformat.h"
      24             : #include "internal.h"
      25             : #include "yuv4mpeg.h"
      26             : 
      27             : #define Y4M_LINE_MAX 256
      28             : 
      29           1 : static int yuv4_generate_header(AVFormatContext *s, char* buf)
      30             : {
      31             :     AVStream *st;
      32             :     int width, height;
      33             :     int raten, rated, aspectn, aspectd, n;
      34             :     char inter;
      35           1 :     const char *colorspace = "";
      36             :     int field_order;
      37             : 
      38           1 :     st     = s->streams[0];
      39           1 :     width  = st->codecpar->width;
      40           1 :     height = st->codecpar->height;
      41           1 :     field_order = st->codecpar->field_order;
      42             : 
      43             :     // TODO: should be avg_frame_rate
      44           1 :     av_reduce(&raten, &rated, st->time_base.den,
      45           1 :               st->time_base.num, (1UL << 31) - 1);
      46             : 
      47           1 :     aspectn = st->sample_aspect_ratio.num;
      48           1 :     aspectd = st->sample_aspect_ratio.den;
      49             : 
      50           1 :     if (aspectn == 0 && aspectd == 1)
      51           1 :         aspectd = 0;  // 0:0 means unknown
      52             : 
      53             : #if FF_API_LAVF_AVCTX
      54             :     FF_DISABLE_DEPRECATION_WARNINGS
      55           1 :     if (field_order != st->codec->field_order && st->codec->field_order != AV_FIELD_UNKNOWN)
      56           0 :         field_order = st->codec->field_order;
      57             :     FF_ENABLE_DEPRECATION_WARNINGS
      58             : #endif
      59             : 
      60           1 :     switch (field_order) {
      61           0 :     case AV_FIELD_TB:
      62           0 :     case AV_FIELD_TT: inter = 't'; break;
      63           0 :     case AV_FIELD_BT:
      64           0 :     case AV_FIELD_BB: inter = 'b'; break;
      65           1 :     default:          inter = 'p'; break;
      66             :     }
      67             : 
      68           1 :     switch (st->codecpar->format) {
      69           0 :     case AV_PIX_FMT_GRAY8:
      70           0 :         colorspace = " Cmono";
      71           0 :         break;
      72           0 :     case AV_PIX_FMT_GRAY9:
      73           0 :         colorspace = " Cmono9";
      74           0 :         break;
      75           0 :     case AV_PIX_FMT_GRAY10:
      76           0 :         colorspace = " Cmono10";
      77           0 :         break;
      78           0 :     case AV_PIX_FMT_GRAY12:
      79           0 :         colorspace = " Cmono12";
      80           0 :         break;
      81           0 :     case AV_PIX_FMT_GRAY16:
      82           0 :         colorspace = " Cmono16";
      83           0 :         break;
      84           0 :     case AV_PIX_FMT_YUV411P:
      85           0 :         colorspace = " C411 XYSCSS=411";
      86           0 :         break;
      87           1 :     case AV_PIX_FMT_YUV420P:
      88           1 :         switch (st->codecpar->chroma_location) {
      89           0 :         case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
      90           0 :         case AVCHROMA_LOC_LEFT:    colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
      91           1 :         default:                   colorspace = " C420jpeg XYSCSS=420JPEG";   break;
      92             :         }
      93           1 :         break;
      94           0 :     case AV_PIX_FMT_YUV422P:
      95           0 :         colorspace = " C422 XYSCSS=422";
      96           0 :         break;
      97           0 :     case AV_PIX_FMT_YUV444P:
      98           0 :         colorspace = " C444 XYSCSS=444";
      99           0 :         break;
     100           0 :     case AV_PIX_FMT_YUV420P9:
     101           0 :         colorspace = " C420p9 XYSCSS=420P9";
     102           0 :         break;
     103           0 :     case AV_PIX_FMT_YUV422P9:
     104           0 :         colorspace = " C422p9 XYSCSS=422P9";
     105           0 :         break;
     106           0 :     case AV_PIX_FMT_YUV444P9:
     107           0 :         colorspace = " C444p9 XYSCSS=444P9";
     108           0 :         break;
     109           0 :     case AV_PIX_FMT_YUV420P10:
     110           0 :         colorspace = " C420p10 XYSCSS=420P10";
     111           0 :         break;
     112           0 :     case AV_PIX_FMT_YUV422P10:
     113           0 :         colorspace = " C422p10 XYSCSS=422P10";
     114           0 :         break;
     115           0 :     case AV_PIX_FMT_YUV444P10:
     116           0 :         colorspace = " C444p10 XYSCSS=444P10";
     117           0 :         break;
     118           0 :     case AV_PIX_FMT_YUV420P12:
     119           0 :         colorspace = " C420p12 XYSCSS=420P12";
     120           0 :         break;
     121           0 :     case AV_PIX_FMT_YUV422P12:
     122           0 :         colorspace = " C422p12 XYSCSS=422P12";
     123           0 :         break;
     124           0 :     case AV_PIX_FMT_YUV444P12:
     125           0 :         colorspace = " C444p12 XYSCSS=444P12";
     126           0 :         break;
     127           0 :     case AV_PIX_FMT_YUV420P14:
     128           0 :         colorspace = " C420p14 XYSCSS=420P14";
     129           0 :         break;
     130           0 :     case AV_PIX_FMT_YUV422P14:
     131           0 :         colorspace = " C422p14 XYSCSS=422P14";
     132           0 :         break;
     133           0 :     case AV_PIX_FMT_YUV444P14:
     134           0 :         colorspace = " C444p14 XYSCSS=444P14";
     135           0 :         break;
     136           0 :     case AV_PIX_FMT_YUV420P16:
     137           0 :         colorspace = " C420p16 XYSCSS=420P16";
     138           0 :         break;
     139           0 :     case AV_PIX_FMT_YUV422P16:
     140           0 :         colorspace = " C422p16 XYSCSS=422P16";
     141           0 :         break;
     142           0 :     case AV_PIX_FMT_YUV444P16:
     143           0 :         colorspace = " C444p16 XYSCSS=444P16";
     144           0 :         break;
     145             :     }
     146             : 
     147             :     /* construct stream header, if this is the first frame */
     148           1 :     n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
     149             :                  Y4M_MAGIC, width, height, raten, rated, inter,
     150             :                  aspectn, aspectd, colorspace);
     151             : 
     152           1 :     return n;
     153             : }
     154             : 
     155          25 : static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
     156             : {
     157          25 :     AVStream *st = s->streams[pkt->stream_index];
     158          25 :     AVIOContext *pb = s->pb;
     159             :     AVFrame *frame;
     160          25 :     int* first_pkt = s->priv_data;
     161             :     int width, height, h_chroma_shift, v_chroma_shift;
     162             :     int i;
     163             :     char buf2[Y4M_LINE_MAX + 1];
     164             :     uint8_t *ptr, *ptr1, *ptr2;
     165             : 
     166          25 :     frame = (AVFrame *)pkt->data;
     167             : 
     168             :     /* for the first packet we have to output the header as well */
     169          25 :     if (*first_pkt) {
     170           1 :         *first_pkt = 0;
     171           1 :         if (yuv4_generate_header(s, buf2) < 0) {
     172           0 :             av_log(s, AV_LOG_ERROR,
     173             :                    "Error. YUV4MPEG stream header write failed.\n");
     174           0 :             return AVERROR(EIO);
     175             :         } else {
     176           1 :             avio_write(pb, buf2, strlen(buf2));
     177             :         }
     178             :     }
     179             : 
     180             :     /* construct frame header */
     181             : 
     182          25 :     avio_printf(s->pb, "%s\n", Y4M_FRAME_MAGIC);
     183             : 
     184          25 :     width  = st->codecpar->width;
     185          25 :     height = st->codecpar->height;
     186             : 
     187          25 :     ptr = frame->data[0];
     188             : 
     189          25 :     switch (st->codecpar->format) {
     190          25 :     case AV_PIX_FMT_GRAY8:
     191             :     case AV_PIX_FMT_YUV411P:
     192             :     case AV_PIX_FMT_YUV420P:
     193             :     case AV_PIX_FMT_YUV422P:
     194             :     case AV_PIX_FMT_YUV444P:
     195          25 :         break;
     196           0 :     case AV_PIX_FMT_GRAY9:
     197             :     case AV_PIX_FMT_GRAY10:
     198             :     case AV_PIX_FMT_GRAY12:
     199             :     case AV_PIX_FMT_GRAY16:
     200             :     case AV_PIX_FMT_YUV420P9:
     201             :     case AV_PIX_FMT_YUV422P9:
     202             :     case AV_PIX_FMT_YUV444P9:
     203             :     case AV_PIX_FMT_YUV420P10:
     204             :     case AV_PIX_FMT_YUV422P10:
     205             :     case AV_PIX_FMT_YUV444P10:
     206             :     case AV_PIX_FMT_YUV420P12:
     207             :     case AV_PIX_FMT_YUV422P12:
     208             :     case AV_PIX_FMT_YUV444P12:
     209             :     case AV_PIX_FMT_YUV420P14:
     210             :     case AV_PIX_FMT_YUV422P14:
     211             :     case AV_PIX_FMT_YUV444P14:
     212             :     case AV_PIX_FMT_YUV420P16:
     213             :     case AV_PIX_FMT_YUV422P16:
     214             :     case AV_PIX_FMT_YUV444P16:
     215           0 :         width *= 2;
     216           0 :         break;
     217           0 :     default:
     218           0 :         av_log(s, AV_LOG_ERROR, "The pixel format '%s' is not supported.\n",
     219           0 :                av_get_pix_fmt_name(st->codecpar->format));
     220           0 :         return AVERROR(EINVAL);
     221             :     }
     222             : 
     223        7225 :     for (i = 0; i < height; i++) {
     224        7200 :         avio_write(pb, ptr, width);
     225        7200 :         ptr += frame->linesize[0];
     226             :     }
     227             : 
     228          50 :     if (st->codecpar->format != AV_PIX_FMT_GRAY8 && st->codecpar->format != AV_PIX_FMT_GRAY9 &&
     229          75 :         st->codecpar->format != AV_PIX_FMT_GRAY10 && st->codecpar->format != AV_PIX_FMT_GRAY12 &&
     230          25 :         st->codecpar->format != AV_PIX_FMT_GRAY16) {
     231             :         // Adjust for smaller Cb and Cr planes
     232          25 :         av_pix_fmt_get_chroma_sub_sample(st->codecpar->format, &h_chroma_shift,
     233             :                                          &v_chroma_shift);
     234             :         // Shift right, rounding up
     235          25 :         width  = AV_CEIL_RSHIFT(width,  h_chroma_shift);
     236          25 :         height = AV_CEIL_RSHIFT(height, v_chroma_shift);
     237             : 
     238          25 :         ptr1 = frame->data[1];
     239          25 :         ptr2 = frame->data[2];
     240        3625 :         for (i = 0; i < height; i++) {     /* Cb */
     241        3600 :             avio_write(pb, ptr1, width);
     242        3600 :             ptr1 += frame->linesize[1];
     243             :         }
     244        3625 :         for (i = 0; i < height; i++) {     /* Cr */
     245        3600 :             avio_write(pb, ptr2, width);
     246        3600 :             ptr2 += frame->linesize[2];
     247             :         }
     248             :     }
     249             : 
     250          25 :     return 0;
     251             : }
     252             : 
     253           1 : static int yuv4_write_header(AVFormatContext *s)
     254             : {
     255           1 :     int *first_pkt = s->priv_data;
     256             : 
     257           1 :     if (s->nb_streams != 1)
     258           0 :         return AVERROR(EIO);
     259             : 
     260           1 :     if (s->streams[0]->codecpar->codec_id != AV_CODEC_ID_WRAPPED_AVFRAME) {
     261           0 :         av_log(s, AV_LOG_ERROR, "ERROR: Codec not supported.\n");
     262           0 :         return AVERROR_INVALIDDATA;
     263             :     }
     264             : 
     265           1 :     switch (s->streams[0]->codecpar->format) {
     266           0 :     case AV_PIX_FMT_YUV411P:
     267           0 :         av_log(s, AV_LOG_WARNING, "Warning: generating rarely used 4:1:1 YUV "
     268             :                "stream, some mjpegtools might not work.\n");
     269           0 :         break;
     270           1 :     case AV_PIX_FMT_GRAY8:
     271             :     case AV_PIX_FMT_YUV420P:
     272             :     case AV_PIX_FMT_YUV422P:
     273             :     case AV_PIX_FMT_YUV444P:
     274           1 :         break;
     275           0 :     case AV_PIX_FMT_GRAY9:
     276             :     case AV_PIX_FMT_GRAY10:
     277             :     case AV_PIX_FMT_GRAY12:
     278             :     case AV_PIX_FMT_GRAY16:
     279             :     case AV_PIX_FMT_YUV420P9:
     280             :     case AV_PIX_FMT_YUV422P9:
     281             :     case AV_PIX_FMT_YUV444P9:
     282             :     case AV_PIX_FMT_YUV420P10:
     283             :     case AV_PIX_FMT_YUV422P10:
     284             :     case AV_PIX_FMT_YUV444P10:
     285             :     case AV_PIX_FMT_YUV420P12:
     286             :     case AV_PIX_FMT_YUV422P12:
     287             :     case AV_PIX_FMT_YUV444P12:
     288             :     case AV_PIX_FMT_YUV420P14:
     289             :     case AV_PIX_FMT_YUV422P14:
     290             :     case AV_PIX_FMT_YUV444P14:
     291             :     case AV_PIX_FMT_YUV420P16:
     292             :     case AV_PIX_FMT_YUV422P16:
     293             :     case AV_PIX_FMT_YUV444P16:
     294           0 :         if (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
     295           0 :             av_log(s, AV_LOG_ERROR, "'%s' is not an official yuv4mpegpipe pixel format. "
     296             :                    "Use '-strict -1' to encode to this pixel format.\n",
     297           0 :                    av_get_pix_fmt_name(s->streams[0]->codecpar->format));
     298           0 :             return AVERROR(EINVAL);
     299             :         }
     300           0 :         av_log(s, AV_LOG_WARNING, "Warning: generating non standard YUV stream. "
     301             :                "Mjpegtools will not work.\n");
     302           0 :         break;
     303           0 :     default:
     304           0 :         av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg can only handle "
     305             :                "yuv444p, yuv422p, yuv420p, yuv411p and gray8 pixel formats. "
     306             :                "And using 'strict -1' also yuv444p9, yuv422p9, yuv420p9, "
     307             :                "yuv444p10, yuv422p10, yuv420p10, "
     308             :                "yuv444p12, yuv422p12, yuv420p12, "
     309             :                "yuv444p14, yuv422p14, yuv420p14, "
     310             :                "yuv444p16, yuv422p16, yuv420p16, "
     311             :                "gray9, gray10, gray12 "
     312             :                "and gray16 pixel formats. "
     313             :                "Use -pix_fmt to select one.\n");
     314           0 :         return AVERROR(EIO);
     315             :     }
     316             : 
     317           1 :     *first_pkt = 1;
     318           1 :     return 0;
     319             : }
     320             : 
     321             : AVOutputFormat ff_yuv4mpegpipe_muxer = {
     322             :     .name              = "yuv4mpegpipe",
     323             :     .long_name         = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
     324             :     .extensions        = "y4m",
     325             :     .priv_data_size    = sizeof(int),
     326             :     .audio_codec       = AV_CODEC_ID_NONE,
     327             :     .video_codec       = AV_CODEC_ID_WRAPPED_AVFRAME,
     328             :     .write_header      = yuv4_write_header,
     329             :     .write_packet      = yuv4_write_packet,
     330             : };

Generated by: LCOV version 1.13