From patchwork Wed Aug 10 16:20:59 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 139 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.140.67 with SMTP id o64csp456862vsd; Wed, 10 Aug 2016 09:36:51 -0700 (PDT) X-Received: by 10.28.221.8 with SMTP id u8mr4021596wmg.61.1470847011371; Wed, 10 Aug 2016 09:36:51 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id fr17si40451746wjc.271.2016.08.10.09.36.49; Wed, 10 Aug 2016 09:36:51 -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; 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 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 9DA1268A4D9; Wed, 10 Aug 2016 19:36:45 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-oi0-f68.google.com (mail-oi0-f68.google.com [209.85.218.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D6A16689C1C for ; Wed, 10 Aug 2016 19:36:36 +0300 (EEST) Received: by mail-oi0-f68.google.com with SMTP id s207so4343101oie.0 for ; Wed, 10 Aug 2016 09:36:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id; bh=WzFaLpZj8BJnifsC/ugCabd7EgA2I8ADoZxwSzcUA9E=; b=GigSFcYrEnX88zzUJKHzg69nhvLKBNCbMOTl8n9cGlxRvtv1A11NEw+bSzF19K8RD1 E9qpuDwqzaw/m/bGfjUrHyjbtwHoO9/DN/X1tJ/VkHsexPWWBmY6/WiZ6ljVjCxQ+mK0 EXre4s4gVxj07HMF2bJebF55ft8aFmieG/TbUuUgEWAkhiSDicJEirWSP7rfouXODIdU lYlHfOW7+pmbHxBp8Xz3N4OjzOiubIDi3s0Z/hW9FIm/jfDFcMu4SKqDsnE/CQ1klu/D R6I7Mt8VeN3hJaIUsjQcTzcHocuUJAnn2e/5RQUfBQ81xa8Isv3fjJb5xJMXACknKdbY A+7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id; bh=WzFaLpZj8BJnifsC/ugCabd7EgA2I8ADoZxwSzcUA9E=; b=V3V5IYfFT0A6Uyk377ijJ+gXQp5gFR9OX9BTTBYvGEUOLOpZSGx1LUyLEhgeFWrZl/ kTWbn063gqhp8SqaJsd4ExMxVqp7R/+ezextlnJNmMOsjwlyq1xvBQT2s80TPzzwhTPX OZrZMj6dv1voGM56Ysb54YZ7jOPPS46MDWawJHSqzjC0FjNJDLufRA+D3fc6fRm25qA5 vMqoXnGoZD4H0bWfkvpcznGGJF5mcZUWUswv6/F/N8q4/F/hi/aPqUD5EZm52QaSfgga RnCgM6MFj44jpywHz3+MUp7UlPanmG2uRds7vX8He9D3VA0pcAzmoJNJjYSk3PoSlmn3 Ppyw== X-Gm-Message-State: AEkooutQZoHVctgDGwIOSq5FmlUKRftLx7eic6CBM904xPvZvH1hdXGD1B+lc79tGzhYzA== X-Received: by 10.157.34.135 with SMTP id y7mr2693706ota.111.1470846121937; Wed, 10 Aug 2016 09:22:01 -0700 (PDT) Received: from localhost.localdomain ([181.22.22.122]) by smtp.gmail.com with ESMTPSA id n18sm14146279ota.27.2016.08.10.09.22.00 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 10 Aug 2016 09:22:01 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Wed, 10 Aug 2016 13:20:59 -0300 Message-Id: <20160810162059.1360-1-jamrial@gmail.com> X-Mailer: git-send-email 2.9.1 Subject: [FFmpeg-devel] [PATCH] examples/demuxing_decoding: convert to codecpar 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" Signed-off-by: James Almer --- doc/examples/demuxing_decoding.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/doc/examples/demuxing_decoding.c b/doc/examples/demuxing_decoding.c index 59e0ccc..175185a 100644 --- a/doc/examples/demuxing_decoding.c +++ b/doc/examples/demuxing_decoding.c @@ -148,11 +148,10 @@ static int decode_packet(int *got_frame, int cached) } static int open_codec_context(int *stream_idx, - AVFormatContext *fmt_ctx, enum AVMediaType type) + AVCodecContext **dec_ctx, AVFormatContext *fmt_ctx, enum AVMediaType type) { int ret, stream_index; AVStream *st; - AVCodecContext *dec_ctx = NULL; AVCodec *dec = NULL; AVDictionary *opts = NULL; @@ -166,17 +165,31 @@ static int open_codec_context(int *stream_idx, st = fmt_ctx->streams[stream_index]; /* find decoder for the stream */ - dec_ctx = st->codec; - dec = avcodec_find_decoder(dec_ctx->codec_id); + dec = avcodec_find_decoder(st->codecpar->codec_id); if (!dec) { fprintf(stderr, "Failed to find %s codec\n", av_get_media_type_string(type)); return AVERROR(EINVAL); } + /* Allocate a codec context for the decoder */ + *dec_ctx = avcodec_alloc_context3(dec); + if (!*dec_ctx) { + fprintf(stderr, "Failed to allocate the %s codec context\n", + av_get_media_type_string(type)); + return AVERROR(ENOMEM); + } + + /* Copy codec parameters from input stream to output codec context */ + if ((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) < 0) { + fprintf(stderr, "Failed to copy %s codec parameters from input to output codec context\n", + av_get_media_type_string(type)); + return ret; + } + /* Init the decoders, with or without reference counting */ av_dict_set(&opts, "refcounted_frames", refcount ? "1" : "0", 0); - if ((ret = avcodec_open2(dec_ctx, dec, &opts)) < 0) { + if ((ret = avcodec_open2(*dec_ctx, dec, &opts)) < 0) { fprintf(stderr, "Failed to open %s codec\n", av_get_media_type_string(type)); return ret; @@ -255,9 +268,8 @@ int main (int argc, char **argv) exit(1); } - if (open_codec_context(&video_stream_idx, fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) { + if (open_codec_context(&video_stream_idx, &video_dec_ctx, fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) { video_stream = fmt_ctx->streams[video_stream_idx]; - video_dec_ctx = video_stream->codec; video_dst_file = fopen(video_dst_filename, "wb"); if (!video_dst_file) { @@ -279,9 +291,8 @@ int main (int argc, char **argv) video_dst_bufsize = ret; } - if (open_codec_context(&audio_stream_idx, fmt_ctx, AVMEDIA_TYPE_AUDIO) >= 0) { + if (open_codec_context(&audio_stream_idx, &audio_dec_ctx, fmt_ctx, AVMEDIA_TYPE_AUDIO) >= 0) { audio_stream = fmt_ctx->streams[audio_stream_idx]; - audio_dec_ctx = audio_stream->codec; audio_dst_file = fopen(audio_dst_filename, "wb"); if (!audio_dst_file) { fprintf(stderr, "Could not open destination file %s\n", audio_dst_filename); @@ -369,8 +380,8 @@ int main (int argc, char **argv) } end: - avcodec_close(video_dec_ctx); - avcodec_close(audio_dec_ctx); + avcodec_free_context(&video_dec_ctx); + avcodec_free_context(&audio_dec_ctx); avformat_close_input(&fmt_ctx); if (video_dst_file) fclose(video_dst_file);