From patchwork Sun Apr 15 04:45:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 8451 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.1.70 with SMTP id c67csp2374256jad; Sat, 14 Apr 2018 21:46:18 -0700 (PDT) X-Google-Smtp-Source: AIpwx49xHzKZT9w2/zIKIiVA6swru0KXXHYJlEv+UtOkGJogMBeNWmoQmTnPq+XMRPcjpcYWJ4S/ X-Received: by 10.223.161.23 with SMTP id o23mr6572807wro.97.1523767578038; Sat, 14 Apr 2018 21:46:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523767577; cv=none; d=google.com; s=arc-20160816; b=X10q6vUYF8lbu0RkomwVkxBMufUHfVgnzqov3/shsZV2Rpn7judirnLHwiEfGfUZOo 0O1ZWUUdSZW6wzJXwS0blUv34GEaRBZyExpkbdPWeUQ/kkRapedIfvc3nYgorHXSJFyu GK+fCF7dc9euW684FyurRUAWFuN0gL2RdyeL6l4vUGfM1ziEQVv6YIU+ilMiU0y4LQLv ROkEeviKvxIuQG7WbiGQZjZbIgyXrLAs+ql6U9zclYXYrh//8q7O+DDKTAuWayTgWV+E kWz70GayXdGvTeOe/PuB9Vzd1j60B8dPf8rz9u2aHzITjMqLEbIynRm5XK4Pd6Tz/CQs D13Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=1j3LpPV0dd6Y5+PxlkCNI3VZusPFEx53MYtLVG/r2Zk=; b=s/wGL//ZhJ31RHEWyG1yNUXAbUqHKd+ILZ8XnBI8GQIgWcOwSy9Uez8k0s4pbPny/P qLT1dYKNri/+OYgDu4EoteHMkHN/DejapdrRJFtQ1vBkOCCYV3a+XM5wm+6P9yUEBe0C ogh5ZA5XXt2gPicyNIJjjkQRoCTf2ng7glzb3i+BQAWnHc0pa7jds0NfpTx4dKVIU9yr /RMXFyZkBkVyMzS0b9o/kEL99xT/lRuRj3BGs0efPWOHh6MvTvI5belnIzX5tTea6nBI KFtSv9R7mZZkHr/9aTnNJEghnYgYujUqpRWy+DRuEz809hWcINaKoCvTTsh+9RsGP00m et5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=NkB+s3kc; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 88si6878311wrf.15.2018.04.14.21.46.16; Sat, 14 Apr 2018 21:46:17 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=NkB+s3kc; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D33A8689C93; Sun, 15 Apr 2018 07:45:48 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt0-f193.google.com (mail-qt0-f193.google.com [209.85.216.193]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 02E2168976E for ; Sun, 15 Apr 2018 07:45:41 +0300 (EEST) Received: by mail-qt0-f193.google.com with SMTP id j3so12072925qtn.9 for ; Sat, 14 Apr 2018 21:46:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=bcBv2kDn5nNhO3Zqa5VDQLSlQ3YXcCm+EXVJMf62fLY=; b=NkB+s3kcL1P4/503QUHww7xVD26q36lkr2CfbIXSQVbfXdspKJioKu96TNRV8Ss0Nh 92PHUaCHoiTfMRdczHIYNKPjwwYQ+RkqO/liIJxlwyRGTAwNp6KrDWtkq23QMsKRjxPB TeLK8S9vDxbF7rCN7Js7uuMfgIpYmuh0zmpEdy/02Cefvt0IEy5YDzK5+uSU1kLlfgSX xD62+DYSAlx0GdjcuC/6GPchrQAsnDOh2ix5Rl2Zdd5P0lOnRHp5cJ/Q/CzzZoAwcRUZ Pc6tQQxapBFR1ZAVy9pfvdIV19Y2PWOkDAdN3dPeOjcoZofTT77sUsEZfZXYdUnXmOvr ErZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=bcBv2kDn5nNhO3Zqa5VDQLSlQ3YXcCm+EXVJMf62fLY=; b=s9o9bP5F3LbDZnRC0ozK2EmCsPhwOn7T4TGkb3pJ6ZUjkfS4kBIX2Fxt2cV9skbVlo sn0/z7LgCuv+eySL33IBAy3G4N3r70VXwJS8K4uUzCzJXPQ/mPoVO586DChVa1MHXeko q+nj0jCmnypk5nsOEFecWKxqYTIz0YsU6YfVjw+SpQ75SBzKu6SrLnz//4PZcIU552AW Omg+khj21J2Q78PE1Lh1zkmMUVFY6/PQxBngIMvJsLc75NwXGELTeXRezcFDxrk0xc7C bHF4+iyFsj7ZT4lYzgWeLv62eBBB7vSRYtiX8Ax9e4pcCrzBJWRDafeowZFaEv2P9927 w4tg== X-Gm-Message-State: ALQs6tDgwCTHALkBthMRkzH5fBDeMxgHjk/pe4DUZPXvF2ROr7LN82dM tl6ghcR6bqZvD1k2NXeiireSig== X-Received: by 10.200.15.252 with SMTP id f57mr10415149qtk.171.1523767565883; Sat, 14 Apr 2018 21:46:05 -0700 (PDT) Received: from localhost.localdomain ([190.188.171.140]) by smtp.gmail.com with ESMTPSA id j1sm4294228qtc.53.2018.04.14.21.46.03 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 14 Apr 2018 21:46:04 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Sun, 15 Apr 2018 01:45:48 -0300 Message-Id: <20180415044548.7240-1-jamrial@gmail.com> X-Mailer: git-send-email 2.16.2 Subject: [FFmpeg-devel] [PATCH] avformat/flacenc: support writing attached pictures X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Rodger Combs Signed-off-by: James Almer --- Changed to not try to mux any kind of video as cover art and instead only bother with streams marked as attached pictures. Also using each stream's priv_data to hold the picture instead of an array in the format's priv_data now. Will push tomorrow or Monday, as this has been rotting on the ml for a while and has also been requested to be in the 4.0 release. libavformat/flacenc.c | 276 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 240 insertions(+), 36 deletions(-) diff --git a/libavformat/flacenc.c b/libavformat/flacenc.c index b894f9ef61..6bbe4b8b3e 100644 --- a/libavformat/flacenc.c +++ b/libavformat/flacenc.c @@ -21,10 +21,13 @@ #include "libavutil/channel_layout.h" #include "libavutil/opt.h" +#include "libavutil/pixdesc.h" #include "libavcodec/flac.h" #include "avformat.h" #include "avio_internal.h" #include "flacenc.h" +#include "id3v2.h" +#include "internal.h" #include "vorbiscomment.h" #include "libavcodec/bytestream.h" @@ -33,8 +36,15 @@ typedef struct FlacMuxerContext { const AVClass *class; int write_header; + int audio_stream_idx; + int waiting_pics; + /* audio packets are queued here until we get all the attached pictures */ + AVPacketList *queue, *queue_end; + /* updated streaminfo sent by the encoder at the end */ uint8_t *streaminfo; + + unsigned attached_types; } FlacMuxerContext; static int flac_write_block_padding(AVIOContext *pb, unsigned int n_padding_bytes, @@ -74,31 +84,161 @@ static int flac_write_block_comment(AVIOContext *pb, AVDictionary **m, return 0; } -static int flac_write_header(struct AVFormatContext *s) +static int flac_write_picture(struct AVFormatContext *s, AVPacket *pkt) { - int ret; - int padding = s->metadata_header_padding; - AVCodecParameters *par = s->streams[0]->codecpar; - FlacMuxerContext *c = s->priv_data; - - if (!c->write_header) + FlacMuxerContext *c = s->priv_data; + AVIOContext *pb = s->pb; + const AVPixFmtDescriptor *pixdesc; + const CodecMime *mime = ff_id3v2_mime_tags; + AVDictionaryEntry *e; + const char *mimetype = NULL, *desc = ""; + const AVStream *st = s->streams[pkt->stream_index]; + int i, mimelen, desclen, type = 0; + + if (!pkt->data) return 0; - if (s->nb_streams > 1) { - av_log(s, AV_LOG_ERROR, "only one stream is supported\n"); + while (mime->id != AV_CODEC_ID_NONE) { + if (mime->id == st->codecpar->codec_id) { + mimetype = mime->str; + break; + } + mime++; + } + if (!mimetype) { + av_log(s, AV_LOG_ERROR, "No mimetype is known for stream %d, cannot " + "write an attached picture.\n", st->index); + return AVERROR(EINVAL); + } + mimelen = strlen(mimetype); + + /* get the picture type */ + e = av_dict_get(st->metadata, "comment", NULL, 0); + for (i = 0; e && i < FF_ARRAY_ELEMS(ff_id3v2_picture_types); i++) { + if (!av_strcasecmp(e->value, ff_id3v2_picture_types[i])) { + type = i; + break; + } + } + + if ((c->attached_types & (1 << type)) & 0x6) { + av_log(s, AV_LOG_ERROR, "Duplicate attachment for type '%s'\n", ff_id3v2_picture_types[type]); return AVERROR(EINVAL); } - if (par->codec_id != AV_CODEC_ID_FLAC) { - av_log(s, AV_LOG_ERROR, "unsupported codec\n"); + + if (type == 1 && (st->codecpar->codec_id != AV_CODEC_ID_PNG || + st->codecpar->width != 32 || + st->codecpar->height != 32)) { + av_log(s, AV_LOG_ERROR, "File icon attachment must be a 32x32 PNG"); return AVERROR(EINVAL); } + c->attached_types |= (1 << type); + + /* get the description */ + if ((e = av_dict_get(st->metadata, "title", NULL, 0))) + desc = e->value; + desclen = strlen(desc); + + avio_w8(pb, 0x06); + avio_wb24(pb, 4 + 4 + mimelen + 4 + desclen + 4 + 4 + 4 + 4 + 4 + pkt->size); + + avio_wb32(pb, type); + + avio_wb32(pb, mimelen); + avio_write(pb, mimetype, mimelen); + + avio_wb32(pb, desclen); + avio_write(pb, desc, desclen); + + avio_wb32(pb, st->codecpar->width); + avio_wb32(pb, st->codecpar->height); + if ((pixdesc = av_pix_fmt_desc_get(st->codecpar->format))) + avio_wb32(pb, av_get_bits_per_pixel(pixdesc)); + else + avio_wb32(pb, 0); + avio_wb32(pb, 0); + + avio_wb32(pb, pkt->size); + avio_write(pb, pkt->data, pkt->size); + return 0; +} + +static int flac_finish_header(struct AVFormatContext *s) +{ + int i, ret, padding = s->metadata_header_padding; if (padding < 0) padding = 8192; /* The FLAC specification states that 24 bits are used to represent the * size of a metadata block so we must clip this value to 2^24-1. */ padding = av_clip_uintp2(padding, 24); + for (i = 0; i < s->nb_streams; i++) { + AVStream *st = s->streams[i]; + AVPacket *pkt = st->priv_data; + if (!pkt) + continue; + flac_write_picture(s, pkt); + av_packet_unref(pkt); + } + + ret = flac_write_block_comment(s->pb, &s->metadata, !padding, + s->flags & AVFMT_FLAG_BITEXACT); + if (ret) + return ret; + + /* The command line flac encoder defaults to placing a seekpoint + * every 10s. So one might add padding to allow that later + * but there seems to be no simple way to get the duration here. + * So just add the amount requested by the user. */ + if (padding) + flac_write_block_padding(s->pb, padding, 1); + + return 0; +} + +static int flac_write_header(struct AVFormatContext *s) +{ + AVCodecParameters *par; + FlacMuxerContext *c = s->priv_data; + int ret, i; + + c->audio_stream_idx = -1; + for (i = 0; i < s->nb_streams; i++) { + AVStream *st = s->streams[i]; + if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { + if (c->audio_stream_idx >= 0 || st->codecpar->codec_id != AV_CODEC_ID_FLAC) { + av_log(s, AV_LOG_ERROR, "Invalid audio stream. Exactly one FLAC " + "audio stream is required.\n"); + return AVERROR(EINVAL); + } + par = s->streams[i]->codecpar; + c->audio_stream_idx = i; + } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { + if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC)) { + av_log(s, AV_LOG_WARNING, "Video stream #%d is not an attached picture. Ignoring\n", i); + continue; + } else if (st->codecpar->codec_id == AV_CODEC_ID_GIF) { + av_log(s, AV_LOG_ERROR, "GIF image support is not implemented.\n"); + return AVERROR_PATCHWELCOME; + } else if (!c->write_header) { + av_log(s, AV_LOG_ERROR, "Can't write attached pictures without a header.\n"); + return AVERROR(EINVAL); + } + c->waiting_pics++; + } else { + av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in FLAC.\n"); + return AVERROR(EINVAL); + } + } + if (c->audio_stream_idx < 0) { + av_log(s, AV_LOG_ERROR, "No audio stream present.\n"); + return AVERROR(EINVAL); + } + + if (!c->write_header) + return 0; + ret = ff_flac_write_header(s->pb, par->extradata, par->extradata_size, 0); if (ret) @@ -121,18 +261,51 @@ static int flac_write_header(struct AVFormatContext *s) } } - ret = flac_write_block_comment(s->pb, &s->metadata, !padding, - s->flags & AVFMT_FLAG_BITEXACT); - if (ret) - return ret; + if (!c->waiting_pics) + ret = flac_finish_header(s); - /* The command line flac encoder defaults to placing a seekpoint - * every 10s. So one might add padding to allow that later - * but there seems to be no simple way to get the duration here. - * So just add the amount requested by the user. */ - if (padding) - flac_write_block_padding(s->pb, padding, 1); + return ret; +} + +static int flac_write_audio_packet(struct AVFormatContext *s, AVPacket *pkt) +{ + FlacMuxerContext *c = s->priv_data; + uint8_t *streaminfo; + int streaminfo_size; + + /* check for updated streaminfo */ + streaminfo = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, + &streaminfo_size); + if (streaminfo && streaminfo_size == FLAC_STREAMINFO_SIZE) { + av_freep(&c->streaminfo); + + c->streaminfo = av_malloc(FLAC_STREAMINFO_SIZE); + if (!c->streaminfo) + return AVERROR(ENOMEM); + memcpy(c->streaminfo, streaminfo, FLAC_STREAMINFO_SIZE); + } + if (pkt->size) + avio_write(s->pb, pkt->data, pkt->size); + return 0; +} + +static int flac_queue_flush(AVFormatContext *s) +{ + FlacMuxerContext *c = s->priv_data; + AVPacket pkt; + int ret, write = 1; + + ret = flac_finish_header(s); + if (ret < 0) + write = 0; + + while (c->queue) { + ff_packet_list_get(&c->queue, &c->queue_end, &pkt); + if (write && (ret = flac_write_audio_packet(s, &pkt)) < 0) + write = 0; + av_packet_unref(&pkt); + } return ret; } @@ -142,7 +315,13 @@ static int flac_write_trailer(struct AVFormatContext *s) int64_t file_size; FlacMuxerContext *c = s->priv_data; uint8_t *streaminfo = c->streaminfo ? c->streaminfo : - s->streams[0]->codecpar->extradata; + s->streams[c->audio_stream_idx]->codecpar->extradata; + + if (c->waiting_pics) { + av_log(s, AV_LOG_WARNING, "No packets were sent for some of the " + "attached pictures.\n"); + flac_queue_flush(s); + } if (!c->write_header || !streaminfo) return 0; @@ -166,23 +345,48 @@ static int flac_write_trailer(struct AVFormatContext *s) static int flac_write_packet(struct AVFormatContext *s, AVPacket *pkt) { FlacMuxerContext *c = s->priv_data; - uint8_t *streaminfo; - int streaminfo_size; + int ret; - /* check for updated streaminfo */ - streaminfo = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, - &streaminfo_size); - if (streaminfo && streaminfo_size == FLAC_STREAMINFO_SIZE) { - av_freep(&c->streaminfo); + if (pkt->stream_index == c->audio_stream_idx) { + if (c->waiting_pics) { + /* buffer audio packets until we get all the pictures */ + ret = ff_packet_list_put(&c->queue, &c->queue_end, pkt, FF_PACKETLIST_FLAG_REF_PACKET); + if (ret < 0) { + av_log(s, AV_LOG_ERROR, "Out of memory in packet queue; skipping attached pictures\n"); + c->waiting_pics = 0; + ret = flac_queue_flush(s); + if (ret < 0) + return ret; + return flac_write_audio_packet(s, pkt); + } + } else + return flac_write_audio_packet(s, pkt); + } else { + AVStream *st = s->streams[pkt->stream_index]; - c->streaminfo = av_malloc(FLAC_STREAMINFO_SIZE); - if (!c->streaminfo) - return AVERROR(ENOMEM); - memcpy(c->streaminfo, streaminfo, FLAC_STREAMINFO_SIZE); + if (!c->waiting_pics || + !(st->disposition & AV_DISPOSITION_ATTACHED_PIC)) + return 0; + + /* warn only once for each stream */ + if (st->nb_frames == 1) { + av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d," + " ignoring.\n", pkt->stream_index); + } + if (st->nb_frames >= 1) + return 0; + + st->priv_data = av_packet_clone(pkt); + if (!st->priv_data) + return ret; + c->waiting_pics--; + + /* flush the buffered audio packets */ + if (!c->waiting_pics && + (ret = flac_queue_flush(s)) < 0) + return ret; } - if (pkt->size) - avio_write(s->pb, pkt->data, pkt->size); return 0; } @@ -205,7 +409,7 @@ AVOutputFormat ff_flac_muxer = { .mime_type = "audio/x-flac", .extensions = "flac", .audio_codec = AV_CODEC_ID_FLAC, - .video_codec = AV_CODEC_ID_NONE, + .video_codec = AV_CODEC_ID_PNG, .write_header = flac_write_header, .write_packet = flac_write_packet, .write_trailer = flac_write_trailer,