Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void video_parser::process_data(own_data *cd, const char *video_type) { // own_data has a std::vector<unsigned char> data
- AVCodec *codec;
- AVCodecContext *codec_context = NULL;
- AVPacket packet;
- AVFrame *frame;
- av_init_packet(&packet);
- if (strcmp(video_type, "mpeg1") == 0) {
- codec = avcodec_find_decoder(CODEC_ID_MPEG1VIDEO);
- }
- if (strcmp(video_type, "mp4") == 0) {
- codec = avcodec_find_decoder(CODEC_ID_MPEG4);
- }
- if (!codec) {
- std::cerr << "codec not found\n";
- exit(1);
- }
- codec_context = avcodec_alloc_context3(codec);
- if (!codec_context) {
- std::cerr << "could not allocate video codec context\n";
- exit(1);
- }
- frame = avcodec_alloc_frame();
- if (codec->capabilities & CODEC_CAP_TRUNCATED) {
- codec_context->flags |= CODEC_FLAG_TRUNCATED;
- }
- if (avcodec_open2(codec_context, codec, NULL) < 0) {
- std::cerr << "could not open codec\n";
- exit(1);
- }
- frame = avcodec_alloc_frame();
- if (!frame) {
- std::cerr << "could not allocate frame\n";
- exit(1);
- }
- int frame_count = 0;
- int got_frame;
- bool wrote_frame_to_disk = false;
- if (cd->get_buff_size() <= 0) {
- std::cerr << "no data available\n";
- exit(1);
- }
- //
- packet.size = cd->get_buff_size();
- packet.data = &cd->data[0];
- std::cout <<"packet.size: " << packet.size << std::endl;
- codec_context->width = 640;
- codec_context->height = 480;
- while (packet.size > 0) {
- int len = avcodec_decode_video2(codec_context, frame, &got_frame, &packet);
- std::cout <<"len: " << len << std::endl;
- if (len < 0) {
- std::cerr << "error while decoding frame " << frame_count << "\n";
- exit(1);
- }
- if (got_frame) {
- // do stuff
- frame_count++;
- }
- if (packet.data) {
- packet.size -= len;
- packet.data += len;
- }
- }
- std::cout << "found " << frame_count << " frames \n";
- packet.data = NULL;
- packet.size = 0;
- avcodec_close(codec_context);
- av_free(codec_context);
- avcodec_free_frame(&frame);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement