[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; |
---|
[6003] | 43 | |
---|
[5975] | 44 | if (filename != NULL) |
---|
[5962] | 45 | this->loadMedia(filename); |
---|
| 46 | |
---|
[5937] | 47 | } |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * Default destructor |
---|
| 51 | */ |
---|
| 52 | MediaContainer::~MediaContainer() |
---|
| 53 | { |
---|
[6068] | 54 | // Free the RGB image |
---|
| 55 | delete [] buffer; |
---|
| 56 | av_free(RGB_frame); |
---|
[5937] | 57 | |
---|
[6013] | 58 | /* Free the frame */ |
---|
[6003] | 59 | av_free(frame); |
---|
| 60 | |
---|
| 61 | /* Close the codec */ |
---|
| 62 | avcodec_close(codec_context); |
---|
| 63 | |
---|
| 64 | /* Close the video file */ |
---|
| 65 | av_close_input_file(format_context); |
---|
| 66 | |
---|
[5937] | 67 | } |
---|
| 68 | |
---|
[6068] | 69 | GLuint MediaContainer::getFrame(int frame_number) |
---|
[5937] | 70 | { |
---|
| 71 | |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | GLuint MediaContainer::getNextFrame() |
---|
| 75 | { |
---|
[6068] | 76 | /* get next frame */ |
---|
| 77 | if(av_read_frame(format_context, &packet) >= 0) |
---|
| 78 | { |
---|
| 79 | //this->printPacketInformation(); |
---|
[5937] | 80 | |
---|
[6068] | 81 | /* Is this a packet from the video stream? */ |
---|
| 82 | if(packet.stream_index == video_stream) |
---|
| 83 | { |
---|
| 84 | int frame_finished; |
---|
| 85 | // Decode video frame |
---|
| 86 | avcodec_decode_video(codec_context, frame, &frame_finished, packet.data, packet.size); |
---|
[6003] | 87 | |
---|
[6068] | 88 | // Did we get a video frame? |
---|
| 89 | if(frame_finished) |
---|
| 90 | { |
---|
| 91 | current_frame++; |
---|
| 92 | PRINTF(1)("current_frame: %i\n", current_frame); |
---|
[6003] | 93 | |
---|
[6068] | 94 | // Convert the image from its native format to RGB |
---|
| 95 | img_convert((AVPicture*)RGB_frame, PIX_FMT_RGB24, (AVPicture*)frame, codec_context->pix_fmt, |
---|
| 96 | codec_context->width, codec_context->height); |
---|
| 97 | |
---|
| 98 | // save frame |
---|
| 99 | this->saveCurrentFrame(); |
---|
[5937] | 100 | |
---|
[6057] | 101 | |
---|
[6068] | 102 | /* RGB_picture -> texture */ |
---|
[6057] | 103 | |
---|
| 104 | |
---|
[6068] | 105 | return texture; |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | // Free the packet that was allocated by av_read_frame |
---|
| 109 | av_free_packet(&packet); |
---|
| 110 | } |
---|
| 111 | else |
---|
| 112 | return NULL; |
---|
| 113 | } |
---|
[6057] | 114 | |
---|
[6068] | 115 | void MediaContainer::saveCurrentFrame() |
---|
| 116 | { |
---|
| 117 | FILE *file; |
---|
| 118 | char filename[32]; |
---|
| 119 | int y; |
---|
[6057] | 120 | |
---|
[6068] | 121 | picture = (AVPicture*)RGB_frame; |
---|
[6057] | 122 | |
---|
[6068] | 123 | // Open file |
---|
| 124 | sprintf(filename, "frame%i.ppm", current_frame); |
---|
| 125 | file = fopen(filename, "wb"); |
---|
| 126 | if(file == NULL) |
---|
| 127 | return; |
---|
[6057] | 128 | |
---|
[6068] | 129 | // Write header |
---|
| 130 | fprintf(file, "P6\n%d %d\n255\n", codec_context->width, codec_context->height); |
---|
| 131 | // Write pixel data |
---|
| 132 | for(y = 0; y < codec_context->height; y++) |
---|
| 133 | fwrite(picture->data[0]+y * picture->linesize[0], 1, codec_context->width*3, file); |
---|
| 134 | // Close file |
---|
| 135 | fclose(file); |
---|
| 136 | |
---|
| 137 | PRINTF(1)("created file: %s\n", filename); |
---|
[6003] | 138 | } |
---|
| 139 | |
---|
[5975] | 140 | void MediaContainer::loadMedia(const char* filename) |
---|
[5937] | 141 | { |
---|
[5975] | 142 | /* Open video file */ |
---|
| 143 | if (av_open_input_file(&format_context, filename, NULL, 0, NULL) !=0 ) |
---|
| 144 | PRINTF(1)("Could not open %s\n", filename); |
---|
[5937] | 145 | |
---|
[6013] | 146 | /* Retrieve stream information */ |
---|
[6003] | 147 | if (av_find_stream_info(format_context) < 0) |
---|
[6013] | 148 | PRINTF(1)("Could not find stream information in %s\n", filename); |
---|
[6003] | 149 | |
---|
| 150 | // Dump information about file onto standard error |
---|
| 151 | //dump_format(pFormatCtx, 0, argv[1], false); |
---|
| 152 | |
---|
| 153 | /* Find the first video stream and take it */ |
---|
| 154 | video_stream = -1; |
---|
| 155 | for(int i = 0; i < format_context->nb_streams; i++) |
---|
[6013] | 156 | { |
---|
| 157 | // NOTE: different code for the 0.4.9-pre1 release of ffmpeg (tardis) |
---|
| 158 | // if(format_context->streams[i]->codec.codec_type == CODEC_TYPE_VIDEO) |
---|
| 159 | if(format_context->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) |
---|
[6003] | 160 | { |
---|
[6013] | 161 | video_stream = i; |
---|
[6003] | 162 | break; |
---|
| 163 | } |
---|
[6013] | 164 | } |
---|
[6003] | 165 | |
---|
[6013] | 166 | if(video_stream == -1) |
---|
| 167 | PRINTF(1)("Could not find a video stream in %s\n", filename); |
---|
[6003] | 168 | |
---|
[6013] | 169 | /* Get a pointer to the codec context for the video stream */ |
---|
| 170 | // NOTE: different code for the 0.4.9-pre1 release of ffmpeg (tardis) |
---|
| 171 | // codec_context = &format_context->streams[video_stream]->codec; |
---|
[6003] | 172 | codec_context = format_context->streams[video_stream]->codec; |
---|
| 173 | |
---|
[6013] | 174 | /* Find the decoder for the video stream */ |
---|
[6003] | 175 | codec = avcodec_find_decoder(codec_context->codec_id); |
---|
| 176 | if (codec == NULL) |
---|
[6013] | 177 | PRINTF(1)("Could not find codec\n"); |
---|
[6003] | 178 | |
---|
[6013] | 179 | /* Open codec */ |
---|
[6003] | 180 | if (avcodec_open(codec_context, codec) < 0) |
---|
[6068] | 181 | PRINTF(1)("Could not open codec\n"); |
---|
[6003] | 182 | |
---|
[6068] | 183 | // Allocate video frame |
---|
| 184 | frame = avcodec_alloc_frame(); |
---|
| 185 | RGB_frame = avcodec_alloc_frame(); |
---|
| 186 | |
---|
| 187 | // Determine required buffer size and allocate buffer |
---|
| 188 | num_bytes = avpicture_get_size(PIX_FMT_RGB24, codec_context->width, codec_context->height); |
---|
| 189 | buffer=new uint8_t[num_bytes]; |
---|
| 190 | |
---|
| 191 | // Assign appropriate parts of buffer to image planes in pFrameRGB |
---|
| 192 | avpicture_fill((AVPicture *)RGB_frame, buffer, PIX_FMT_RGB24, codec_context->width, codec_context->height); |
---|
| 193 | |
---|
[5937] | 194 | } |
---|
| 195 | |
---|
| 196 | int MediaContainer::getHeight() |
---|
| 197 | { |
---|
[6068] | 198 | return codec_context->height; |
---|
[5937] | 199 | } |
---|
| 200 | |
---|
| 201 | int MediaContainer::getWidth() |
---|
| 202 | { |
---|
[6068] | 203 | return codec_context->width; |
---|
[5937] | 204 | } |
---|
| 205 | |
---|
| 206 | int MediaContainer::getFrameRate() |
---|
| 207 | { |
---|
| 208 | |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | void MediaContainer::getStream(/* stream */) |
---|
| 212 | { |
---|
| 213 | |
---|
| 214 | } |
---|
[6057] | 215 | |
---|
[6068] | 216 | void MediaContainer::printMediaInformation() |
---|
[6057] | 217 | { |
---|
[6068] | 218 | PRINTF(1)("========================\n"); |
---|
| 219 | PRINTF(1)("========================\n"); |
---|
| 220 | PRINTF(1)("= MEDIACONTAINER =\n"); |
---|
| 221 | PRINTF(1)("========================\n"); |
---|
| 222 | PRINTF(1)("========================\n"); |
---|
| 223 | PRINTF(1)("= AVFormatContext =\n"); |
---|
| 224 | PRINTF(1)("========================\n"); |
---|
| 225 | PRINTF(1)("filename: %s\n", format_context->filename); |
---|
| 226 | PRINTF(1)("nb_streams: %i\n", format_context->nb_streams); |
---|
| 227 | PRINTF(1)("duration: %fs\n", format_context->duration/1000000.); |
---|
| 228 | PRINTF(1)("file_size: %ikb\n", format_context->file_size/1024); |
---|
| 229 | PRINTF(1)("bit_rate: %ikb/s\n", format_context->bit_rate/1000); |
---|
| 230 | PRINTF(1)("nb_frames: %i\n", format_context->streams[video_stream]->nb_frames); |
---|
| 231 | PRINTF(1)("r_frame_rate: %i\n", format_context->streams[video_stream]->r_frame_rate.num); |
---|
| 232 | PRINTF(1)("========================\n"); |
---|
| 233 | PRINTF(1)("= AVCodecContext =\n"); |
---|
| 234 | PRINTF(1)("========================\n"); |
---|
| 235 | PRINTF(1)("width: %i\n", codec_context->width); |
---|
| 236 | PRINTF(1)("height: %i\n", codec_context->height); |
---|
| 237 | PRINTF(1)("========================\n"); |
---|
| 238 | PRINTF(1)("= AVCodec =\n"); |
---|
| 239 | PRINTF(1)("========================\n"); |
---|
| 240 | PRINTF(1)("codec name: %s\n", codec->name); |
---|
| 241 | PRINTF(1)("========================\n"); |
---|
| 242 | PRINTF(1)("========================\n"); |
---|
[6057] | 243 | } |
---|
| 244 | |
---|
[6068] | 245 | void MediaContainer::printPacketInformation() |
---|
[6057] | 246 | { |
---|
[6068] | 247 | PRINTF(1)("========================\n"); |
---|
| 248 | PRINTF(1)("========================\n"); |
---|
| 249 | PRINTF(1)("= AVPacket =\n"); |
---|
| 250 | PRINTF(1)("========================\n"); |
---|
| 251 | PRINTF(1)("pts: %i\n", packet.pts); |
---|
| 252 | PRINTF(1)("dts: %i\n", packet.dts); |
---|
| 253 | PRINTF(1)("size: %i\n", packet.size); |
---|
| 254 | PRINTF(1)("stream_index: %i\n", packet.stream_index); |
---|
| 255 | PRINTF(1)("duration: %i\n", packet.duration); |
---|
| 256 | PRINTF(1)("pos: %i\n", packet.pos); |
---|
| 257 | PRINTF(1)("========================\n"); |
---|
| 258 | PRINTF(1)("========================\n"); |
---|
[6057] | 259 | } |
---|