• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
arthur__

FFMPEG and av_open_input_stream

3 posts in this topic

Hello,

I try to read a mpeg2 video from a stream with ffmpeg library. In my first tests, I tried to read from a byte buffer, but now I read directly from a file (in my ReadPacket function) in order to be sure it's right data.
I tried many way to do that, but av_open_input_stream function return always -1.
I post my code here and hope someone could help me ! If someone have a full code to do that, i''ll be happy to see it. Thanks.

[code]av_log_set_level(AV_LOG_VERBOSE);
avcodec_init();
av_register_all();
avcodec_register_all();

pFormatCtx = avformat_alloc_context();
if (!pFormatCtx)
{
return -1;
}

FILE* f = fopen("D:\\projets\\test_data\\video\\video_ts.mpg", "rb");

int bioBufferSize = 8000;
unsigned char *bioBuffer = (unsigned char *)av_malloc(bioBufferSize + FF_INPUT_BUFFER_PADDING_SIZE);
ByteIOContext *bioContext = new ByteIOContext();
int ret = init_put_byte(bioContext, bioBuffer, bioBufferSize, 0, (void *)f, readPackets, NULL, seekPackets);

AVFormatParameters params, *ap = &params;
memset(ap, 0, sizeof(*ap));
ap->prealloced_context = 1

//pFormatCtx->video_codec_id = findCodecOrDie("mpeg2video", AVMEDIA_TYPE_VIDEO, 0, avcodecOpts[AVMEDIA_TYPE_VIDEO]->strict_std_compliance);
//pFormatCtx->flags |= AVFMT_FLAG_NONBLOCK;
AVInputFormat *inputFormat = av_find_input_format("mpegts");
//pFormatCtx->iformat = inputFormat;

ret = av_open_input_stream(&pFormatCtx, bioContext, "stream", inputFormat, ap);

av_close_input_stream(pFormatCtx);
av_free(bioBuffer);
delete bioContext;
fclose(f);

[...]
[/code]

And my readPacket func :
[code]

static int readPackets(void *opaque, uint8_t *buf, int buf_size)
{
FILE* f = (FILE *)opaque;
int read = fread(buf, 1, buf_size, f);
return read;
}
[/code]
0

Share this post


Link to post
Share on other sites
Why not use FFmpeg to open the video file? I hate giving out code, but this is what I use to open and play video files. It accepts anything FFmpeg can read, not just mpeg2video. It's a copy 'n' paste of a small portion of my code, and for my personal purposes I just needed the frame to be an 8-bit gray frame so I convert all frames to 8-bit gray. If there's a particular part you have a question about, feel free to ask. Otherwise, I'm going to assume the code is self-explanatory.

[code]
Video::Video(const std::string& filename) : imageConvertContext(NULL),
buffer(NULL),
grayFrame(NULL),
frame(NULL),
codecContext(NULL),
formatContext(NULL),
videoStreamIndex(-1),
endReached(true),
scaledWidth(0),
scaledHeight(0),
currentFrameNumber(-1)
{
int errorCode = 0;

if ((errorCode = avformat_open_input(&formatContext, filename.c_str(), NULL, NULL)) != 0)
{
throw std::runtime_error("Unable to open the stream");
}

if ((errorCode = avformat_find_stream_info(formatContext, NULL)) < 0)
{
throw std::runtime_error("Unable to find the stream's info");
}

for (unsigned int i = 0; i < formatContext->nb_streams; ++i)
{
if (formatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoStreamIndex = i;
break;
}
}

if (videoStreamIndex == -1)
{
throw std::runtime_error("Unable to find a video stream");
}

codecContext = formatContext->streams[videoStreamIndex]->codec;
codec = avcodec_find_decoder(codecContext->codec_id);
if (codec == NULL)
{
throw std::runtime_error("Unsupported codec");
}

if ((errorCode = avcodec_open2(codecContext, codec, NULL)) != 0)
{
throw std::runtime_error("Error opening the codec");
}

frameRate = formatContext->streams[videoStreamIndex]->avg_frame_rate;
if (!frameRate.num || ! frameRate.den)
{
throw std::runtime_error("This video does not have a constant frame rate");
}

//TODO: if one of these fail, we should release what has succeeded
frame = avcodec_alloc_frame();
grayFrame = avcodec_alloc_frame();
if (frame == NULL || grayFrame == NULL)
{
throw std::runtime_error("Unable to allocate frame memory");
}

int numBytes = avpicture_get_size(PIX_FMT_GRAY8, codecContext->width, codecContext->height);
buffer = (uint8_t*)av_malloc(numBytes * sizeof(uint8_t));

avpicture_fill((AVPicture*)grayFrame, buffer, PIX_FMT_GRAY8, codecContext->width, codecContext->height);

imageConvertContext = sws_getContext(codecContext->width,
codecContext->height,
codecContext->pix_fmt,
codecContext->width,
codecContext->height,
PIX_FMT_GRAY8,
SWS_FAST_BILINEAR,
NULL,
NULL,
NULL);

scaledWidth = codecContext->width;
scaledHeight = codecContext->height;

endReached = false;
}

Video::~Video()
{
sws_freeContext(imageConvertContext);
av_free_packet(&packet);
av_free(buffer);
av_free(grayFrame);
av_free(frame);
avcodec_close(codecContext);
av_close_input_file(formatContext);
}

const AVFrame* Video::getNextFrame()
{
while (av_read_frame(formatContext, &packet) == 0)
{
int frameFinished = 0;
if (packet.stream_index == videoStreamIndex)
{
avcodec_decode_video2(codecContext, frame, &frameFinished, &packet);
if (frameFinished)
{
sws_scale(imageConvertContext,
frame->data,
frame->linesize,
0,
frame->height,
grayFrame->data,
grayFrame->linesize);

++currentFrameNumber;
return grayFrame;
}
}
}

endReached = true;
return NULL;
}[/code]
0

Share this post


Link to post
Share on other sites
Thanks for your reply;
I want to read from stream (in a char *buffer), but as it not work I changed my buffer by reading directly the file, that's why I use [color=#000000][size=2][left]av_open_input_stream. [/left][/size][/color]I have already tried to open my file with av_open_input_file and it's work, but it's not my goal here.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0