[5937] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: David Hasenfratz, Stephan Lienhard |
---|
| 13 | co-programmer: |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_MEDIA module |
---|
| 19 | For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput |
---|
| 20 | */ |
---|
| 21 | #define DEBUG_MODULE_MEDIA |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | /* include your own header */ |
---|
| 25 | #include "media_container.h" |
---|
| 26 | |
---|
| 27 | /* header for debug output */ |
---|
| 28 | #include "debug.h" |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * Default constructor |
---|
| 33 | */ |
---|
[5939] | 34 | MediaContainer::MediaContainer(const char* filename) |
---|
[5937] | 35 | { |
---|
[5975] | 36 | /* set the class id for the base object */ |
---|
| 37 | this->setClassID(CL_MEDIA_CONTAINER, "MediaContainer"); |
---|
| 38 | |
---|
[5962] | 39 | /* register all formats and codecs */ |
---|
| 40 | av_register_all(); |
---|
[5937] | 41 | |
---|
[6013] | 42 | current_frame = 0; |
---|
| 43 | num_frames = 0; |
---|
[6057] | 44 | frames_left = true; |
---|
[6003] | 45 | |
---|
[5975] | 46 | if (filename != NULL) |
---|
[5962] | 47 | this->loadMedia(filename); |
---|
| 48 | |
---|
[5937] | 49 | } |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | * Default destructor |
---|
| 53 | */ |
---|
| 54 | MediaContainer::~MediaContainer() |
---|
| 55 | { |
---|
| 56 | |
---|
[6013] | 57 | /* Free the frame */ |
---|
[6003] | 58 | av_free(frame); |
---|
| 59 | |
---|
| 60 | /* Close the codec */ |
---|
| 61 | avcodec_close(codec_context); |
---|
| 62 | |
---|
| 63 | /* Close the video file */ |
---|
| 64 | av_close_input_file(format_context); |
---|
| 65 | |
---|
[5937] | 66 | } |
---|
| 67 | |
---|
[6003] | 68 | /*GLuint MediaContainer::getFrame(int frame_number) |
---|
[5937] | 69 | { |
---|
| 70 | |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | GLuint MediaContainer::getNextFrame() |
---|
| 74 | { |
---|
| 75 | |
---|
[6003] | 76 | }*/ |
---|
| 77 | |
---|
| 78 | SDL_Surface* MediaContainer::getFrame(int frame_number) |
---|
| 79 | { |
---|
| 80 | |
---|
[5937] | 81 | } |
---|
| 82 | |
---|
[6003] | 83 | SDL_Surface* MediaContainer::getNextFrame() |
---|
| 84 | { |
---|
[6057] | 85 | /* get next frame */ |
---|
| 86 | if(av_read_frame(format_context, &packet) >= 0) |
---|
| 87 | { |
---|
| 88 | frames_left = true; |
---|
| 89 | current_frame++; |
---|
| 90 | PRINTF(1)("current_frame: %i\n", current_frame); |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | /* work with the frame */ |
---|
| 94 | /* packet -> SDL_Surface */ |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | } |
---|
| 100 | else |
---|
| 101 | frames_left = false; |
---|
| 102 | |
---|
[6003] | 103 | } |
---|
| 104 | |
---|
[5975] | 105 | void MediaContainer::loadMedia(const char* filename) |
---|
[5937] | 106 | { |
---|
[5975] | 107 | /* Open video file */ |
---|
| 108 | if (av_open_input_file(&format_context, filename, NULL, 0, NULL) !=0 ) |
---|
| 109 | PRINTF(1)("Could not open %s\n", filename); |
---|
[5937] | 110 | |
---|
[6013] | 111 | /* Retrieve stream information */ |
---|
[6003] | 112 | if (av_find_stream_info(format_context) < 0) |
---|
[6013] | 113 | PRINTF(1)("Could not find stream information in %s\n", filename); |
---|
[6003] | 114 | |
---|
| 115 | // Dump information about file onto standard error |
---|
| 116 | //dump_format(pFormatCtx, 0, argv[1], false); |
---|
| 117 | |
---|
| 118 | /* Find the first video stream and take it */ |
---|
| 119 | video_stream = -1; |
---|
| 120 | for(int i = 0; i < format_context->nb_streams; i++) |
---|
[6013] | 121 | { |
---|
| 122 | // NOTE: different code for the 0.4.9-pre1 release of ffmpeg (tardis) |
---|
| 123 | // if(format_context->streams[i]->codec.codec_type == CODEC_TYPE_VIDEO) |
---|
| 124 | if(format_context->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) |
---|
[6003] | 125 | { |
---|
[6013] | 126 | video_stream = i; |
---|
[6003] | 127 | break; |
---|
| 128 | } |
---|
[6013] | 129 | } |
---|
[6003] | 130 | |
---|
[6013] | 131 | if(video_stream == -1) |
---|
| 132 | PRINTF(1)("Could not find a video stream in %s\n", filename); |
---|
[6003] | 133 | |
---|
[6013] | 134 | /* Get a pointer to the codec context for the video stream */ |
---|
| 135 | // NOTE: different code for the 0.4.9-pre1 release of ffmpeg (tardis) |
---|
| 136 | // codec_context = &format_context->streams[video_stream]->codec; |
---|
[6003] | 137 | codec_context = format_context->streams[video_stream]->codec; |
---|
| 138 | |
---|
[6013] | 139 | /* Find the decoder for the video stream */ |
---|
[6003] | 140 | codec = avcodec_find_decoder(codec_context->codec_id); |
---|
| 141 | if (codec == NULL) |
---|
[6013] | 142 | PRINTF(1)("Could not find codec\n"); |
---|
[6003] | 143 | |
---|
[6013] | 144 | /* Open codec */ |
---|
[6003] | 145 | if (avcodec_open(codec_context, codec) < 0) |
---|
[6013] | 146 | PRINTF(1)("Could not open codec\n"); |
---|
[6003] | 147 | |
---|
[5937] | 148 | } |
---|
| 149 | |
---|
| 150 | int MediaContainer::getHeight() |
---|
| 151 | { |
---|
[6057] | 152 | return codec_context->height; |
---|
[5937] | 153 | } |
---|
| 154 | |
---|
| 155 | int MediaContainer::getWidth() |
---|
| 156 | { |
---|
[6057] | 157 | return codec_context->width; |
---|
[5937] | 158 | } |
---|
| 159 | |
---|
| 160 | int MediaContainer::getFrameRate() |
---|
| 161 | { |
---|
| 162 | |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | void MediaContainer::getStream(/* stream */) |
---|
| 166 | { |
---|
| 167 | |
---|
| 168 | } |
---|
[6057] | 169 | |
---|
| 170 | bool MediaContainer::framesLeft() |
---|
| 171 | { |
---|
| 172 | return frames_left; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | void MediaContainer::printMediaInformation() |
---|
| 176 | { |
---|
| 177 | PRINTF(1)("========================\n"); |
---|
| 178 | PRINTF(1)("========================\n"); |
---|
| 179 | PRINTF(1)("= MEDIACONTAINER =\n"); |
---|
| 180 | PRINTF(1)("========================\n"); |
---|
| 181 | PRINTF(1)("========================\n"); |
---|
| 182 | PRINTF(1)("= AVFormatContext =\n"); |
---|
| 183 | PRINTF(1)("========================\n"); |
---|
| 184 | PRINTF(1)("filename: %s\n", format_context->filename); |
---|
| 185 | PRINTF(1)("nb_streams: %i\n", format_context->nb_streams); |
---|
| 186 | PRINTF(1)("duration: %fs\n", format_context->duration/1000000.); |
---|
| 187 | PRINTF(1)("file_size: %ikb\n", format_context->file_size/1024); |
---|
| 188 | PRINTF(1)("bit_rate: %ikb/s\n", format_context->bit_rate/1000); |
---|
| 189 | PRINTF(1)("nb_frames: %i\n", format_context->streams[video_stream]->nb_frames); |
---|
| 190 | //PRINTF(1)("r_frame_rate: %ifps\n", format_context->streams[video_stream]->r_frame_rate.num); |
---|
| 191 | PRINTF(1)("========================\n"); |
---|
| 192 | PRINTF(1)("= AVCodecContext =\n"); |
---|
| 193 | PRINTF(1)("========================\n"); |
---|
| 194 | PRINTF(1)("width: %i\n", codec_context->width); |
---|
| 195 | PRINTF(1)("height: %i\n", codec_context->height); |
---|
| 196 | PRINTF(1)("========================\n"); |
---|
| 197 | PRINTF(1)("= AVCodec =\n"); |
---|
| 198 | PRINTF(1)("========================\n"); |
---|
| 199 | PRINTF(1)("codec name: %s\n", codec->name); |
---|
| 200 | PRINTF(1)("========================\n"); |
---|
| 201 | PRINTF(1)("========================\n"); |
---|
| 202 | } |
---|