From patchwork Sun Apr 22 15:29:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 8579 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:155:0:0:0:0:0 with SMTP id c82-v6csp2855541jad; Sun, 22 Apr 2018 08:33:09 -0700 (PDT) X-Google-Smtp-Source: AIpwx48JTxFUOWWJIVIPfX6xfSIO4H8WOfiYjOopRQWcwdNBi+KpzL8xS0KPJrzR4NTUmNfznKTB X-Received: by 2002:adf:87d4:: with SMTP id c20-v6mr15333545wrc.197.1524411189313; Sun, 22 Apr 2018 08:33:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524411189; cv=none; d=google.com; s=arc-20160816; b=UXpYmOs7RyNKjMexiEnRRDPj5upaTbzAyMaKQrK16HIY1rrUt+q1LYAiOIISckQnRh CBqLXjj+0krAxdMLITUI1qbAOU3QsAEmdzuQMmjgmVzdSCTh6J4SToLPwWufRjUOw4NP Zjp16+KyObL+H99SSrI+F2oWtH5ckBy9aSou4N7+kMjqX1eAJOMT7QBiOvgcnnVGswgw v3IX/MZT72icqHR+jeWFHWhhBJvDSd4HWWegSzr2TBN6ms1Kn6YkPLu49JXS5XZhe++N Y4gyxPKbqdPo9fs4VuXZiFk4ffsisscPDEBj/9/pQ5O6XpKk7CcRoEekTAbMBPPnDyRe Ziig== 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:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=eU+C0vT2VPmMpa3hoMrdprO0omCF0OnBCJ7eUzj2kPA=; b=Wkf6uyZsEIG5775YeJxVdt3oQsfeVhgZM0U3hSMCUYzZ5OmfMGqbRxPIEIpxbRWCGn T3ZicE/ZUkbVUdBgSczc/BWQoR6uL3EdXooA33dzWikG3sNfvxFwE2uwsiUl5VStiLsq DLwx7bTU6yG9fcmkC1sxmE43vnhq/PuQ+Hgz1Uy+umiQ7ZM6vWp2dXT1V2pmqcoNW9jk SeGJ1vJRXf4iHaR67XlybxYSdleowg4dfjIlFoTdzJuqVI5RzAb6wHyehrRVd/mzTIEr 5EgLuEfHDr0md48sC/UQkZl1y43KiVCNl8AwvjtRDp3fEN/IsTgToNCVmuoLw6wQCSHN XLXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@jkqxz-net.20150623.gappssmtp.com header.s=20150623 header.b=vcfvMzca; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id v11-v6si8120667wri.218.2018.04.22.08.33.08; Sun, 22 Apr 2018 08:33:09 -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=@jkqxz-net.20150623.gappssmtp.com header.s=20150623 header.b=vcfvMzca; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5795468A3B6; Sun, 22 Apr 2018 18:29:26 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr0-f173.google.com (mail-wr0-f173.google.com [209.85.128.173]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 90B2C68A2C1 for ; Sun, 22 Apr 2018 18:29:23 +0300 (EEST) Received: by mail-wr0-f173.google.com with SMTP id q3-v6so24568687wrj.6 for ; Sun, 22 Apr 2018 08:29:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references; bh=/qUtQHQJamsT/TXGmylsY1XvOFO81iMD8rariRN9Pws=; b=vcfvMzcaGb+IVj7RFCt9wTkEGiLYqxyBgikLcXB+mlMn7V6lIFyj54dpXi3Hu6mhrO bqWNqeDDqaLJyEAieUORjswLDwfQ9fNAGCnXwdCbh+O4XssXxKqEu6ogQ/5sfR515h5j kmYLEXHPWQZ4ARew6sOzAphCVqHkPE9iS6JMVOY4Rntjedi1Acs+d+9KV4f8VjrM7/Fu yis0aKtU2pv1WoXLklf9GxMR0q5TBbJLubPTNn9Dg+JIynbcmjxmAMAi0qYUMtPzZuZu 6ioBtK+yRYEGk3+1l1Wumwhrr1hukLQouG3aRb+a5+RqjUilHHDGC7nbfJbhbB8kJGQY 9kSQ== 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:in-reply-to :references; bh=/qUtQHQJamsT/TXGmylsY1XvOFO81iMD8rariRN9Pws=; b=k51TgXNjEvmppuMUt2mCofKGg68LRTFmAdthgsjUqk4/h6qcRL6axoODV59QIyrhCp naHV0uLhYhdPZwiVSadh0cF1J8rEWDMnHamqwFIF9u8kaz8hDIBPKJc/Xf2agzwoWGqG Q1spKK0uwdB2jJMgtTPmEcKcH6Zpo0rt4uQ+mF64xHQFrBGhn7S9hWcsq7h7qwqWTRR+ ekmoFUMGoWXvTujCAyPEO7KT18lnNCHngy9sqScy42yljNOqDGhhm8V+bgBbOIvDf4Pb Z6XJLgecfu+GrARHbWCe/M4jbDPlmZ0ngNjmTQsHTWs5q+3ZOU2gAoAqerV1r0RzYoAf Id/g== X-Gm-Message-State: ALQs6tDMWyc4D/KBTf7hoDX6J5XB53h/Oqe0QdrmMxvmb2Z+QUxOxU/k jL5YoSEECWMQ/ZpFld2y3Jxsg+WV X-Received: by 2002:adf:ad61:: with SMTP id p88-v6mr14298939wrc.24.1524410991469; Sun, 22 Apr 2018 08:29:51 -0700 (PDT) Received: from rywe.jkqxz.net (cpc91242-cmbg18-2-0-cust650.5-4.cable.virginm.net. [82.8.130.139]) by smtp.gmail.com with ESMTPSA id 32-v6sm7021498wrf.79.2018.04.22.08.29.50 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 22 Apr 2018 08:29:50 -0700 (PDT) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Sun, 22 Apr 2018 16:29:18 +0100 Message-Id: <20180422152921.32510-24-sw@jkqxz.net> X-Mailer: git-send-email 2.16.3 In-Reply-To: <20180422152921.32510-1-sw@jkqxz.net> References: <20180422152921.32510-1-sw@jkqxz.net> Subject: [FFmpeg-devel] [PATCH 23/26] lavc/cbs: Add JPEG support 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" --- configure | 4 +- libavcodec/Makefile | 1 + libavcodec/cbs.c | 6 + libavcodec/cbs_internal.h | 1 + libavcodec/cbs_jpeg.c | 487 ++++++++++++++++++++++++++++++++++ libavcodec/cbs_jpeg.h | 128 +++++++++ libavcodec/cbs_jpeg_syntax_template.c | 191 +++++++++++++ 7 files changed, 817 insertions(+), 1 deletion(-) create mode 100644 libavcodec/cbs_jpeg.c create mode 100644 libavcodec/cbs_jpeg.h create mode 100644 libavcodec/cbs_jpeg_syntax_template.c diff --git a/configure b/configure index dee507cb6a..5bbeb83d13 100755 --- a/configure +++ b/configure @@ -2235,6 +2235,7 @@ CONFIG_EXTRA=" cbs cbs_h264 cbs_h265 + cbs_jpeg cbs_mpeg2 dirac_parse dvprofile @@ -2496,6 +2497,7 @@ threads_if_any="$THREADS_LIST" # subsystems cbs_h264_select="cbs golomb" cbs_h265_select="cbs golomb" +cbs_jpeg_select="cbs" cbs_mpeg2_select="cbs" dct_select="rdft" dirac_parse_select="golomb" @@ -2925,7 +2927,7 @@ mjpeg_cuvid_decoder_deps="cuvid" mjpeg_qsv_encoder_deps="libmfx" mjpeg_qsv_encoder_select="qsvenc" mjpeg_vaapi_encoder_deps="VAEncPictureParameterBufferJPEG" -mjpeg_vaapi_encoder_select="vaapi_encode jpegtables" +mjpeg_vaapi_encoder_select="cbs_jpeg jpegtables vaapi_encode" mpeg1_cuvid_decoder_deps="cuvid" mpeg1_v4l2m2m_decoder_deps="v4l2_m2m mpeg1_v4l2_m2m" mpeg2_crystalhd_decoder_select="crystalhd" diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 4b8ad121db..be7466ce95 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -64,6 +64,7 @@ OBJS-$(CONFIG_CABAC) += cabac.o OBJS-$(CONFIG_CBS) += cbs.o OBJS-$(CONFIG_CBS_H264) += cbs_h2645.o h2645_parse.o OBJS-$(CONFIG_CBS_H265) += cbs_h2645.o h2645_parse.o +OBJS-$(CONFIG_CBS_JPEG) += cbs_jpeg.o OBJS-$(CONFIG_CBS_MPEG2) += cbs_mpeg2.o OBJS-$(CONFIG_CRYSTALHD) += crystalhd.o OBJS-$(CONFIG_DCT) += dct.o dct32_fixed.o dct32_float.o diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c index 897e0bb28e..e21a67f601 100644 --- a/libavcodec/cbs.c +++ b/libavcodec/cbs.c @@ -35,6 +35,9 @@ static const CodedBitstreamType *cbs_type_table[] = { #if CONFIG_CBS_H265 &ff_cbs_type_h265, #endif +#if CONFIG_CBS_JPEG + &ff_cbs_type_jpeg, +#endif #if CONFIG_CBS_MPEG2 &ff_cbs_type_mpeg2, #endif @@ -47,6 +50,9 @@ const enum AVCodecID ff_cbs_all_codec_ids[] = { #if CONFIG_CBS_H265 AV_CODEC_ID_H265, #endif +#if CONFIG_CBS_JPEG + AV_CODEC_ID_MJPEG, +#endif #if CONFIG_CBS_MPEG2 AV_CODEC_ID_MPEG2VIDEO, #endif diff --git a/libavcodec/cbs_internal.h b/libavcodec/cbs_internal.h index be540e2a44..a338b82ef3 100644 --- a/libavcodec/cbs_internal.h +++ b/libavcodec/cbs_internal.h @@ -86,6 +86,7 @@ int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc, extern const CodedBitstreamType ff_cbs_type_h264; extern const CodedBitstreamType ff_cbs_type_h265; +extern const CodedBitstreamType ff_cbs_type_jpeg; extern const CodedBitstreamType ff_cbs_type_mpeg2; diff --git a/libavcodec/cbs_jpeg.c b/libavcodec/cbs_jpeg.c new file mode 100644 index 0000000000..89f27063da --- /dev/null +++ b/libavcodec/cbs_jpeg.c @@ -0,0 +1,487 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "cbs.h" +#include "cbs_internal.h" +#include "cbs_jpeg.h" + + +#define HEADER(name) do { \ + ff_cbs_trace_header(ctx, name); \ + } while (0) + +#define CHECK(call) do { \ + err = (call); \ + if (err < 0) \ + return err; \ + } while (0) + + +#define READ +#define READWRITE read +#define RWContext GetBitContext +#define FUNC(name) cbs_jpeg_read_ ## name + +#define u(width, name, range_min, range_max) do { \ + uint32_t value = range_min; \ + CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \ + &value, range_min, range_max)); \ + current->name = value; \ + } while (0) + +#include "cbs_jpeg_syntax_template.c" + +#undef READ +#undef READWRITE +#undef RWContext +#undef FUNC +#undef u + +#define WRITE +#define READWRITE write +#define RWContext PutBitContext +#define FUNC(name) cbs_jpeg_write_ ## name + +#define u(width, name, range_min, range_max) do { \ + uint32_t value = current->name; \ + CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \ + value, range_min, range_max)); \ + } while (0) + + +#include "cbs_jpeg_syntax_template.c" + +#undef READ +#undef READWRITE +#undef RWContext +#undef FUNC +#undef u + + +static void cbs_jpeg_free_application_data(void *unit, uint8_t *content) +{ + JPEGRawApplicationData *ad = (JPEGRawApplicationData*)content; + av_buffer_unref(&ad->Ap_ref); + av_freep(&content); +} + +static void cbs_jpeg_free_scan(void *unit, uint8_t *content) +{ + JPEGRawScan *scan = (JPEGRawScan*)content; + av_buffer_unref(&scan->data_ref); + av_freep(&content); +} + +static int cbs_jpeg_split_fragment(CodedBitstreamContext *ctx, + CodedBitstreamFragment *frag, + int header) +{ + uint8_t *unit_data; + size_t unit_size; + int unit, start, end, marker, next_start, next_marker; + int err, i, j; + + if (frag->data_size < 4) { + // Definitely too short to be meaningful. + return AVERROR_INVALIDDATA; + } + + for (i = 0; i + 1 < frag->data_size && frag->data[i] != 0xff; i++); + if (i > 0) { + av_log(ctx->log_ctx, AV_LOG_WARNING, "Discarding %d bytes at " + "beginning of image.\n", i); + } + for (++i; i + 1 < frag->data_size && frag->data[i] == 0xff; i++); + if (i + 1 >= frag->data_size && frag->data[i]) { + av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid JPEG image: " + "no SOI marker found.\n"); + return AVERROR_INVALIDDATA; + } + marker = frag->data[i]; + if (marker != JPEG_MARKER_SOI) { + av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid JPEG image: first " + "marker is %02x, should be SOI.\n", marker); + return AVERROR_INVALIDDATA; + } + for (++i; i + 1 < frag->data_size && frag->data[i] == 0xff; i++); + if (i + 1 >= frag->data_size) { + av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid JPEG image: " + "no image content found.\n"); + return AVERROR_INVALIDDATA; + } + marker = frag->data[i]; + start = i + 1; + + for (unit = 0;; unit++) { + if (marker == JPEG_MARKER_EOI) { + break; + } else if (marker == JPEG_MARKER_SOS) { + for (i = start; i + 1 < frag->data_size; i++) { + if (frag->data[i] != 0xff) + continue; + end = i; + for (++i; i + 1 < frag->data_size && + frag->data[i] == 0xff; i++); + if (i + 1 >= frag->data_size) { + next_marker = -1; + } else { + if (frag->data[i] == 0x00) + continue; + next_marker = frag->data[i]; + next_start = i + 1; + } + break; + } + } else { + int length; + i = start; + if (i + 2 > frag->data_size) + break; + length = frag->data[i] << 8 | frag->data[i + 1]; + av_log(ctx->log_ctx, AV_LOG_DEBUG, "Length = %d (i = %d)\n", length, i); + if (i + length > frag->data_size) + break; + end = start + length; + + i = end; + if (frag->data[i] != 0xff) { + next_marker = -1; + } else { + for (++i; i + 1 < frag->data_size && + frag->data[i] == 0xff; i++); + if (i + 1 >= frag->data_size) { + next_marker = -1; + } else { + next_marker = frag->data[i]; + next_start = i + 1; + } + } + } + + av_log(ctx->log_ctx, AV_LOG_DEBUG, "start = %d, end = %d, marker = %02x\n", + start, end, marker); + + unit_size = end - start; + unit_data = av_malloc(unit_size + AV_INPUT_BUFFER_PADDING_SIZE); + if (!unit_data) + return AVERROR(ENOMEM); + if (marker == JPEG_MARKER_SOS) { + for (i = start, j = 0; i < end; i++, j++) { + if (frag->data[i] == 0xff) { + while (frag->data[i] == 0xff) + ++i; + unit_data[j] = 0xff; + } else { + unit_data[j] = frag->data[i]; + } + } + unit_size = j; + } else { + memcpy(unit_data, frag->data + start, unit_size); + } + memset(unit_data + unit_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); + + err = ff_cbs_insert_unit_data(ctx, frag, unit, marker, + unit_data, unit_size, NULL); + if (err < 0) { + av_free(&unit_data); + return err; + } + + if (next_marker == -1) + break; + marker = next_marker; + start = next_start; + } + + return 0; +} + +static int cbs_jpeg_read_unit(CodedBitstreamContext *ctx, + CodedBitstreamUnit *unit) +{ + GetBitContext gbc; + int err; + + err = init_get_bits(&gbc, unit->data, 8 * unit->data_size); + if (err < 0) + return err; + + if (unit->type >= JPEG_MARKER_SOF0 && + unit->type <= JPEG_MARKER_SOF3) { + err = ff_cbs_alloc_unit_content(ctx, unit, + sizeof(JPEGRawFrameHeader), + NULL); + if (err < 0) + return err; + + err = cbs_jpeg_read_frame_header(ctx, &gbc, unit->content); + if (err < 0) + return err; + + } else if (unit->type >= JPEG_MARKER_APPN && + unit->type <= JPEG_MARKER_APPN + 15) { + err = ff_cbs_alloc_unit_content(ctx, unit, + sizeof(JPEGRawApplicationData), + &cbs_jpeg_free_application_data); + if (err < 0) + return err; + + err = cbs_jpeg_read_application_data(ctx, &gbc, unit->content); + if (err < 0) + return err; + + } else if (unit->type == JPEG_MARKER_SOS) { + JPEGRawScan *scan; + int pos; + + err = ff_cbs_alloc_unit_content(ctx, unit, + sizeof(JPEGRawScan), + &cbs_jpeg_free_scan); + if (err < 0) + return err; + scan = unit->content; + + err = cbs_jpeg_read_scan_header(ctx, &gbc, &scan->header); + if (err < 0) + return err; + + pos = get_bits_count(&gbc); + av_assert0(pos % 8 == 0); + if (pos > 0) { + scan->data_size = unit->data_size - pos / 8; + scan->data_ref = av_buffer_ref(unit->data_ref); + if (!scan->data_ref) + return AVERROR(ENOMEM); + scan->data = unit->data + pos / 8; + } + + } else { + switch (unit->type) { +#define SEGMENT(marker, type, func) \ + case JPEG_MARKER_ ## marker: \ + { \ + err = ff_cbs_alloc_unit_content(ctx, unit, \ + sizeof(type), NULL); \ + if (err < 0) \ + return err; \ + err = cbs_jpeg_read_ ## func(ctx, &gbc, unit->content); \ + if (err < 0) \ + return err; \ + } \ + break + SEGMENT(DQT, JPEGRawQuantisationTableSpecification, dqt); + SEGMENT(DHT, JPEGRawHuffmanTableSpecification, dht); + SEGMENT(COM, JPEGRawComment, comment); +#undef SEGMENT + default: + return AVERROR(ENOSYS); + } + } + + return 0; +} + +static int cbs_jpeg_write_scan(CodedBitstreamContext *ctx, + CodedBitstreamUnit *unit, + PutBitContext *pbc) +{ + JPEGRawScan *scan = unit->content; + int i, err; + + err = cbs_jpeg_write_scan_header(ctx, pbc, &scan->header); + if (err < 0) + return err; + + if (scan->data) { + if (scan->data_size * 8 > put_bits_left(pbc)) + return AVERROR(ENOSPC); + + for (i = 0; i < scan->data_size; i++) + put_bits(pbc, 8, scan->data[i]); + } + + return 0; +} + +static int cbs_jpeg_write_segment(CodedBitstreamContext *ctx, + CodedBitstreamUnit *unit, + PutBitContext *pbc) +{ + int err; + + if (unit->type >= JPEG_MARKER_SOF0 && + unit->type <= JPEG_MARKER_SOF3) { + err = cbs_jpeg_write_frame_header(ctx, pbc, unit->content); + } else if (unit->type >= JPEG_MARKER_APPN && + unit->type <= JPEG_MARKER_APPN + 15) { + err = cbs_jpeg_write_application_data(ctx, pbc, unit->content); + } else { + switch (unit->type) { +#define SEGMENT(marker, func) \ + case JPEG_MARKER_ ## marker: \ + err = cbs_jpeg_write_ ## func(ctx, pbc, unit->content); \ + break; + SEGMENT(DQT, dqt); + SEGMENT(DHT, dht); + SEGMENT(COM, comment); + default: + return AVERROR_PATCHWELCOME; + } + } + + return err; +} + +static int cbs_jpeg_write_unit(CodedBitstreamContext *ctx, + CodedBitstreamUnit *unit) +{ + CodedBitstreamJPEGContext *priv = ctx->priv_data; + PutBitContext pbc; + int err; + + if (!priv->write_buffer) { + // Initial write buffer size is 1MB. + priv->write_buffer_size = 1024 * 1024; + + reallocate_and_try_again: + err = av_reallocp(&priv->write_buffer, priv->write_buffer_size); + if (err < 0) { + av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a " + "sufficiently large write buffer (last attempt " + "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size); + return err; + } + } + + init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size); + + if (unit->type == JPEG_MARKER_SOS) + err = cbs_jpeg_write_scan(ctx, unit, &pbc); + else + err = cbs_jpeg_write_segment(ctx, unit, &pbc); + + if (err == AVERROR(ENOSPC)) { + // Overflow. + priv->write_buffer_size *= 2; + goto reallocate_and_try_again; + } + if (err < 0) { + // Write failed for some other reason. + return err; + } + + if (put_bits_count(&pbc) % 8) + unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8; + else + unit->data_bit_padding = 0; + + unit->data_size = (put_bits_count(&pbc) + 7) / 8; + flush_put_bits(&pbc); + + err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size); + if (err < 0) + return err; + + memcpy(unit->data, priv->write_buffer, unit->data_size); + + return 0; +} + +static int cbs_jpeg_assemble_fragment(CodedBitstreamContext *ctx, + CodedBitstreamFragment *frag) +{ + const CodedBitstreamUnit *unit; + uint8_t *data; + size_t size, dp, sp; + int i; + + size = 4; // SOI + EOI. + for (i = 0; i < frag->nb_units; i++) { + unit = &frag->units[i]; + size += 2 + unit->data_size; + if (unit->type == JPEG_MARKER_SOS) { + for (sp = 0; sp < unit->data_size; sp++) { + if (unit->data[sp] == 0xff) + ++size; + } + } + } + + frag->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE); + if (!frag->data_ref) + return AVERROR(ENOMEM); + data = frag->data_ref->data; + + dp = 0; + + data[dp++] = 0xff; + data[dp++] = JPEG_MARKER_SOI; + + for (i = 0; i < frag->nb_units; i++) { + unit = &frag->units[i]; + + data[dp++] = 0xff; + data[dp++] = unit->type; + + if (unit->type != JPEG_MARKER_SOS) { + for (sp = 0; sp < unit->data_size; sp++) + data[dp++] = unit->data[sp]; + } else { + for (sp = 0; sp < unit->data_size; sp++) { + if (unit->data[sp] == 0xff) { + data[dp++] = 0xff; + data[dp++] = 0x00; + } else { + data[dp++] = unit->data[sp]; + } + } + } + } + + data[dp++] = 0xff; + data[dp++] = JPEG_MARKER_EOI; + + av_assert0(dp == size); + + memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE); + frag->data = data; + frag->data_size = size; + + return 0; +} + +static void cbs_jpeg_close(CodedBitstreamContext *ctx) +{ + CodedBitstreamJPEGContext *priv = ctx->priv_data; + + av_freep(&priv->write_buffer); +} + +const CodedBitstreamType ff_cbs_type_jpeg = { + .codec_id = AV_CODEC_ID_MJPEG, + + .priv_data_size = sizeof(CodedBitstreamJPEGContext), + + .split_fragment = &cbs_jpeg_split_fragment, + .read_unit = &cbs_jpeg_read_unit, + .write_unit = &cbs_jpeg_write_unit, + .assemble_fragment = &cbs_jpeg_assemble_fragment, + + .close = &cbs_jpeg_close, +}; diff --git a/libavcodec/cbs_jpeg.h b/libavcodec/cbs_jpeg.h new file mode 100644 index 0000000000..f427c4c616 --- /dev/null +++ b/libavcodec/cbs_jpeg.h @@ -0,0 +1,128 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_CBS_JPEG_H +#define AVCODEC_CBS_JPEG_H + +#include +#include + + +enum { + JPEG_MARKER_SOF0 = 0xc0, + JPEG_MARKER_SOF1 = 0xc1, + JPEG_MARKER_SOF2 = 0xc2, + JPEG_MARKER_SOF3 = 0xc3, + + JPEG_MARKER_DHT = 0xc4, + JPEG_MARKER_SOI = 0xd8, + JPEG_MARKER_EOI = 0xd9, + JPEG_MARKER_SOS = 0xda, + JPEG_MARKER_DQT = 0xdb, + + JPEG_MARKER_APPN = 0xe0, + JPEG_MARKER_JPGN = 0xf0, + JPEG_MARKER_COM = 0xfe, +}; + +enum { + JPEG_MAX_COMPONENTS = 255, + + JPEG_MAX_HEIGHT = 65535, + JPEG_MAX_WIDTH = 65535, +}; + + +typedef struct JPEGRawFrameHeader { + uint16_t Lf; + uint8_t P; + uint16_t Y; + uint16_t X; + uint16_t Nf; + + uint8_t C [JPEG_MAX_COMPONENTS]; + uint8_t H [JPEG_MAX_COMPONENTS]; + uint8_t V [JPEG_MAX_COMPONENTS]; + uint8_t Tq[JPEG_MAX_COMPONENTS]; +} JPEGRawFrameHeader; + +typedef struct JPEGRawScanHeader { + uint16_t Ls; + uint8_t Ns; + + uint8_t Cs[JPEG_MAX_COMPONENTS]; + uint8_t Td[JPEG_MAX_COMPONENTS]; + uint8_t Ta[JPEG_MAX_COMPONENTS]; + + uint8_t Ss; + uint8_t Se; + uint8_t Ah; + uint8_t Al; +} JPEGRawScanHeader; + +typedef struct JPEGRawScan { + JPEGRawScanHeader header; + uint8_t *data; + size_t data_size; + AVBufferRef *data_ref; +} JPEGRawScan; + +typedef struct JPEGRawQuantisationTable { + uint8_t Pq; + uint8_t Tq; + uint16_t Q[64]; +} JPEGRawQuantisationTable; + +typedef struct JPEGRawQuantisationTableSpecification { + uint16_t Lq; + JPEGRawQuantisationTable table[4]; +} JPEGRawQuantisationTableSpecification; + +typedef struct JPEGRawHuffmanTable { + uint8_t Tc; + uint8_t Th; + uint8_t L[16]; + uint8_t V[224]; +} JPEGRawHuffmanTable; + +typedef struct JPEGRawHuffmanTableSpecification { + uint16_t Lh; + JPEGRawHuffmanTable table[8]; +} JPEGRawHuffmanTableSpecification; + +typedef struct JPEGRawApplicationData { + uint16_t Lp; + uint8_t *Ap; + AVBufferRef *Ap_ref; +} JPEGRawApplicationData; + +typedef struct JPEGRawComment { + uint16_t Lc; + uint8_t *Cm; + AVBufferRef *Cm_ref; +} JPEGRawComment; + + +typedef struct CodedBitstreamJPEGContext { + // Write buffer. + uint8_t *write_buffer; + size_t write_buffer_size; +} CodedBitstreamJPEGContext; + + +#endif /* AVCODEC_CBS_JPEG_H */ diff --git a/libavcodec/cbs_jpeg_syntax_template.c b/libavcodec/cbs_jpeg_syntax_template.c new file mode 100644 index 0000000000..e4f35906b9 --- /dev/null +++ b/libavcodec/cbs_jpeg_syntax_template.c @@ -0,0 +1,191 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +static int FUNC(frame_header)(CodedBitstreamContext *ctx, RWContext *rw, + JPEGRawFrameHeader *current) +{ + int err, i; + + HEADER("Frame Header"); + + u(16, Lf, 8, 8 + 3 * JPEG_MAX_COMPONENTS); + + u(8, P, 2, 16); + u(16, Y, 0, JPEG_MAX_HEIGHT); + u(16, X, 1, JPEG_MAX_WIDTH); + u(8, Nf, 1, JPEG_MAX_COMPONENTS); + + for (i = 0; i < current->Nf; i++) { + u(8, C[i], 0, JPEG_MAX_COMPONENTS); + u(4, H[i], 1, 4); + u(4, V[i], 1, 4); + u(8, Tq[i], 0, 3); + } + + return 0; +} + +static int FUNC(quantisation_table)(CodedBitstreamContext *ctx, RWContext *rw, + JPEGRawQuantisationTable *current) +{ + int err, i; + + u(4, Pq, 0, 1); + u(4, Tq, 0, 3); + + if (current->Pq) { + for (i = 0; i < 64; i++) + u(16, Q[i], 1, 255); + } else { + for (i = 0; i < 64; i++) + u(8, Q[i], 1, 255); + } + + return 0; +} + +static int FUNC(dqt)(CodedBitstreamContext *ctx, RWContext *rw, + JPEGRawQuantisationTableSpecification *current) +{ + int err, i, n; + + HEADER("Quantisation Tables"); + + u(16, Lq, 2, 2 + 4 * 65); + n = current->Lq / 65; + + for (i = 0; i < n; i++) + CHECK(FUNC(quantisation_table)(ctx, rw, ¤t->table[i])); + + return 0; +} + +static int FUNC(huffman_table)(CodedBitstreamContext *ctx, RWContext *rw, + JPEGRawHuffmanTable *current) +{ + int err, i, j, ij; + + u(4, Tc, 0, 1); + u(4, Th, 0, 3); + + for (i = 0; i < 16; i++) + u(8, L[i], 0, 224); + + ij = 0; + for (i = 0; i < 16; i++) { + for (j = 0; j < current->L[i]; j++) { + u(8, V[ij], 0, 255); + ++ij; + } + } + + return 0; +} + +static int FUNC(dht)(CodedBitstreamContext *ctx, RWContext *rw, + JPEGRawHuffmanTableSpecification *current) +{ + int err, i, j, n; + + HEADER("Huffman Tables"); + + u(16, Lh, 2, 2 + 8 * (1 + 16 + 256)); + + n = 2; + for (i = 0; n < current->Lh; i++) { + CHECK(FUNC(huffman_table)(ctx, rw, ¤t->table[i])); + + ++n; + for (j = 0; j < 16; j++) + n += 1 + current->table[i].L[j]; + } + + return 0; +} + +static int FUNC(scan_header)(CodedBitstreamContext *ctx, RWContext *rw, + JPEGRawScanHeader *current) +{ + int err, j; + + HEADER("Scan"); + + u(16, Ls, 6, 6 + 2 * JPEG_MAX_COMPONENTS); + + u(8, Ns, 1, 4); + for (j = 0; j < current->Ns; j++) { + u(8, Cs[j], 0, JPEG_MAX_COMPONENTS); + u(4, Td[j], 0, 3); + u(4, Ta[j], 0, 3); + } + + u(8, Ss, 0, 63); + u(8, Se, 0, 63); + u(4, Ah, 0, 13); + u(4, Al, 0, 15); + + return 0; +} + +static int FUNC(application_data)(CodedBitstreamContext *ctx, RWContext *rw, + JPEGRawApplicationData *current) +{ + int err, i; + + HEADER("Application Data"); + + u(16, Lp, 2, 65535); + + if (current->Lp > 2) { +#ifdef READ + current->Ap_ref = av_buffer_alloc(current->Lp - 2); + if (!current->Ap_ref) + return AVERROR(ENOMEM); + current->Ap = current->Ap_ref->data; +#endif + + for (i = 0; i < current->Lp - 2; i++) + u(8, Ap[i], 0, 255); + } + + return 0; +} + +static int FUNC(comment)(CodedBitstreamContext *ctx, RWContext *rw, + JPEGRawComment *current) +{ + int err, i; + + HEADER("Comment"); + + u(16, Lc, 2, 65535); + + if (current->Lc > 2) { +#ifdef READ + current->Cm_ref = av_buffer_alloc(current->Lc - 2); + if (!current->Cm_ref) + return AVERROR(ENOMEM); + current->Cm = current->Cm_ref->data; +#endif + + for (i = 0; i < current->Lc - 2; i++) + u(8, Cm[i], 0, 255); + } + + return 0; +}