[置顶] avio_reading简单使用
2018-01-30 16:25
471 查看
/*
* Copyright (c) 2011 Reinhard Tartler
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma
comment(lib,
"avformat.lib")
#pragma
comment(lib,
"avcodec.lib")
#pragma
comment(lib,
"avdevice.lib")
#pragma
comment(lib,
"avfilter.lib")
#pragma
comment(lib,
"avutil.lib")
#pragma
comment(lib,
"postproc.lib")
#pragma
comment(lib,
"swresample.lib")
#pragma
comment(lib,
"swscale.lib")
/**
* @file
* libavformat AVIOContext API example.
*
* Make libavformat demuxer access media content through a custom
* AVIOContext read callback.
* @example avio_reading.c
*/
extern
"C" {
#include
<libavcodec/avcodec.h>
#include
<libavformat/avformat.h>
#include
<libavformat/avio.h>
#include
<libavutil/file.h>
}
struct
buffer_data
{
uint8_t
*ptr;
size_t
size;
///< size left in the buffer
};
static
int
read_packet(void
*opaque,
uint8_t
*buf,
int
buf_size)
{
struct
buffer_data
*bd
= (struct
buffer_data
*)opaque;
buf_size
=
FFMIN(buf_size,
bd->size);
printf("ptr:%p
size:%zu\n",
bd->ptr,
bd->size);
/* copy internal buffer data to buf */
memcpy(buf,
bd->ptr,
buf_size);
bd->ptr
+=
buf_size;
bd->size
-=
buf_size;
return
buf_size;
}
int
main(int
argc,
char *argv[])
{
AVFormatContext
*fmt_ctx
=
NULL;
AVIOContext
*avio_ctx
=
NULL;
uint8_t
*buffer
=
NULL, *avio_ctx_buffer
=
NULL;
size_t
buffer_size,
avio_ctx_buffer_size
= 4096;
char
*input_filename
=
"E:/video_source/1.mp4";
int
ret = 0;
struct
buffer_data
bd = { 0 };
/*
if (argc != 2) {
fprintf(stderr, "usage: %s input_file\n"
"API example program to show how to read from a custom buffer "
"accessed through AVIOContext.\n", argv[0]);
return 1;
}*/
/* register codecs and formats and other lavf/lavc components*/
av_register_all();
/* slurp file content into buffer */
ret
=
av_file_map(input_filename,
&buffer, &buffer_size,
0,
NULL);
if
(ret
< 0)
goto
end;
/* fill opaque structure used by the AVIOContext read callback
*/
bd.ptr
=
buffer;
bd.size
=
buffer_size;
if
(!(fmt_ctx
=
avformat_alloc_context())) {
ret
=
AVERROR(ENOMEM);
goto
end;
}
avio_ctx_buffer
= (uint8_t*)av_malloc(avio_ctx_buffer_size);
if
(!avio_ctx_buffer) {
ret
=
AVERROR(ENOMEM);
goto
end;
}
avio_ctx
=
avio_alloc_context(avio_ctx_buffer,
avio_ctx_buffer_size,
0, &bd,
&read_packet,
NULL,
NULL);
if
(!avio_ctx) {
ret
=
AVERROR(ENOMEM);
goto
end;
}
fmt_ctx->pb
=
avio_ctx;
ret
=
avformat_open_input(&fmt_ctx,
NULL,
NULL,
NULL);
if
(ret
< 0) {
fprintf(stderr,
"Could not open input\n");
goto
end;
}
ret
=
avformat_find_stream_info(fmt_ctx,
NULL);
if
(ret
< 0) {
fprintf(stderr,
"Could not find stream information\n");
goto
end;
}
av_dump_format(fmt_ctx,
0,
input_filename, 0);
end:
avformat_close_input(&fmt_ctx);
/* note: the internal buffer could have changed, and be != avio_ctx_buffer
*/
if
(avio_ctx) {
av_freep(&avio_ctx->buffer);
av_freep(&avio_ctx);
}
av_file_unmap(buffer,
buffer_size);
if
(ret
< 0) {
fprintf(stderr,
"Error occurred: %s\n",
av_err2str(ret));
return
1;
}
while
(true)
{
}
return
0;
}
运行结果:
* Copyright (c) 2011 Reinhard Tartler
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma
comment(lib,
"avformat.lib")
#pragma
comment(lib,
"avcodec.lib")
#pragma
comment(lib,
"avdevice.lib")
#pragma
comment(lib,
"avfilter.lib")
#pragma
comment(lib,
"avutil.lib")
#pragma
comment(lib,
"postproc.lib")
#pragma
comment(lib,
"swresample.lib")
#pragma
comment(lib,
"swscale.lib")
/**
* @file
* libavformat AVIOContext API example.
*
* Make libavformat demuxer access media content through a custom
* AVIOContext read callback.
* @example avio_reading.c
*/
extern
"C" {
#include
<libavcodec/avcodec.h>
#include
<libavformat/avformat.h>
#include
<libavformat/avio.h>
#include
<libavutil/file.h>
}
struct
buffer_data
{
uint8_t
*ptr;
size_t
size;
///< size left in the buffer
};
static
int
read_packet(void
*opaque,
uint8_t
*buf,
int
buf_size)
{
struct
buffer_data
*bd
= (struct
buffer_data
*)opaque;
buf_size
=
FFMIN(buf_size,
bd->size);
printf("ptr:%p
size:%zu\n",
bd->ptr,
bd->size);
/* copy internal buffer data to buf */
memcpy(buf,
bd->ptr,
buf_size);
bd->ptr
+=
buf_size;
bd->size
-=
buf_size;
return
buf_size;
}
int
main(int
argc,
char *argv[])
{
AVFormatContext
*fmt_ctx
=
NULL;
AVIOContext
*avio_ctx
=
NULL;
uint8_t
*buffer
=
NULL, *avio_ctx_buffer
=
NULL;
size_t
buffer_size,
avio_ctx_buffer_size
= 4096;
char
*input_filename
=
"E:/video_source/1.mp4";
int
ret = 0;
struct
buffer_data
bd = { 0 };
/*
if (argc != 2) {
fprintf(stderr, "usage: %s input_file\n"
"API example program to show how to read from a custom buffer "
"accessed through AVIOContext.\n", argv[0]);
return 1;
}*/
/* register codecs and formats and other lavf/lavc components*/
av_register_all();
/* slurp file content into buffer */
ret
=
av_file_map(input_filename,
&buffer, &buffer_size,
0,
NULL);
if
(ret
< 0)
goto
end;
/* fill opaque structure used by the AVIOContext read callback
*/
bd.ptr
=
buffer;
bd.size
=
buffer_size;
if
(!(fmt_ctx
=
avformat_alloc_context())) {
ret
=
AVERROR(ENOMEM);
goto
end;
}
avio_ctx_buffer
= (uint8_t*)av_malloc(avio_ctx_buffer_size);
if
(!avio_ctx_buffer) {
ret
=
AVERROR(ENOMEM);
goto
end;
}
avio_ctx
=
avio_alloc_context(avio_ctx_buffer,
avio_ctx_buffer_size,
0, &bd,
&read_packet,
NULL,
NULL);
if
(!avio_ctx) {
ret
=
AVERROR(ENOMEM);
goto
end;
}
fmt_ctx->pb
=
avio_ctx;
ret
=
avformat_open_input(&fmt_ctx,
NULL,
NULL,
NULL);
if
(ret
< 0) {
fprintf(stderr,
"Could not open input\n");
goto
end;
}
ret
=
avformat_find_stream_info(fmt_ctx,
NULL);
if
(ret
< 0) {
fprintf(stderr,
"Could not find stream information\n");
goto
end;
}
av_dump_format(fmt_ctx,
0,
input_filename, 0);
end:
avformat_close_input(&fmt_ctx);
/* note: the internal buffer could have changed, and be != avio_ctx_buffer
*/
if
(avio_ctx) {
av_freep(&avio_ctx->buffer);
av_freep(&avio_ctx);
}
av_file_unmap(buffer,
buffer_size);
if
(ret
< 0) {
fprintf(stderr,
"Error occurred: %s\n",
av_err2str(ret));
return
1;
}
while
(true)
{
}
return
0;
}
运行结果:
相关文章推荐
- [置顶] (一)GreenDao3.0的简单使用
- [置顶] 使用Bootstrap+metisMenu完成简单的后台管理界面
- [置顶] RecycleView的基本配置和使用方法(四)---RecycleView实现简单demo
- [置顶] github简单使用
- [置顶] JSTL的简单使用
- [置顶] Android butterknife简单使用篇
- [置顶] 关于侧滑SlidingMenu的简单使用步骤
- [置顶] css3 befor after 简单使用 制作时尚焦点图相框
- [置顶] 使用红孩儿工具箱完成基于Cocos2d-x的简单游戏动画界面
- [置顶] (一)libevent安装及简单的使用
- [置顶] Android 底部导航栏 BottomNavigationBar的简单使用
- [置顶] Redisson分布式锁的简单使用
- [置顶] [教程]VC++6.0的简单使用
- [置顶] Git的简单使用及远程仓库的推送
- [置顶] java使用数组实现简单的Map
- [置顶] 动画之三种动画的简单使用
- [置顶] 纯手工打造漂亮的垂直时间轴,使用最简单的HTML+CSS+JQUERY完成100个版本更新记录的华丽转身!
- [置顶] EasyMock的简单使用
- [置顶] 使用C语言与栈实现简单多则运算计算器(包括括号优先级运算)
- [置顶] 简单的自定义标题栏(不使用Toolbar)