1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
/*MT*
MediaTomb - http://www.mediatomb.cc/
ffmpeg_handler.cc - this file is part of MediaTomb.
Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
Copyright (C) 2006-2009 Gena Batyan <bgeradz@mediatomb.cc>,
Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
Leonhard Wimmer <leo@mediatomb.cc>
MediaTomb is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
MediaTomb is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
version 2 along with MediaTomb; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
$Id: ffmpeg_handler.cc 2010 2009-01-11 19:10:43Z lww $
*/
/*
This code was contributed by
Copyright (C) 2007 Ingo Preiml <ipreiml@edu.uni-klu.ac.at>
*/
/// \file ffmpeg_handler.cc
/// \brief Implementeation of the FfmpegHandler class.
// Information about the stream are to be found in the structure
// AVFormatContext, defined in libavformat/avformat.h:335
// and in the structure
// AVCodecContext, defined in libavcodec/avcodec.h:722
// in the ffmpeg sources
#ifdef HAVE_CONFIG_H
#include "autoconfig.h"
#endif
#ifdef HAVE_FFMPEG
// ffmpeg needs the following sources
// INT64_C is not defined in ffmpeg/avformat.h but is needed
// macro defines included via autoconfig.h
#include <stdint.h>
//#ifdef FFMPEG_NEEDS_EXTERN_C
extern "C"
{
//#endif
#include AVFORMAT_INCLUDE
//#ifdef FFMPEG_NEEDS_EXTERN_C
} // extern "C"
//#endif
#ifdef HAVE_FFMPEGTHUMBNAILER
#include <libffmpegthumbnailer/videothumbnailerc.h>
#endif
#include "config_manager.h"
#include "ffmpeg_handler.h"
#include "string_converter.h"
#include "common.h"
#include "tools.h"
#include "rexp.h"
//#include "mxml/mxml.h"
#include "mem_io_handler.h"
using namespace zmm;
//using namespace mxml;
// Default constructor
FfmpegHandler::FfmpegHandler() : MetadataHandler()
{
}
static void addFfmpegMetadataFields(Ref<CdsItem> item, AVFormatContext *pFormatCtx)
{
Ref<StringConverter> sc = StringConverter::m2i();
if (strlen(pFormatCtx->title) > 0)
{
log_debug("Added metadata title: %s\n", pFormatCtx->title);
item->setMetadata(String(MT_KEYS[M_TITLE].upnp),
sc->convert(String(pFormatCtx->title)));
}
if (strlen(pFormatCtx->author) > 0)
{
log_debug("Added metadata author: %s\n", pFormatCtx->author);
item->setMetadata(String(MT_KEYS[M_ARTIST].upnp),
sc->convert(String(pFormatCtx->author)));
}
if (strlen(pFormatCtx->album) > 0)
{
log_debug("Added metadata album: %s\n", pFormatCtx->album);
item->setMetadata(String(MT_KEYS[M_ALBUM].upnp),
sc->convert(String(pFormatCtx->album)));
}
if (pFormatCtx->year > 0)
{
log_debug("Added metadata year: %d\n", pFormatCtx->year);
item->setMetadata(String(MT_KEYS[M_DATE].upnp),
sc->convert(String::from(pFormatCtx->year)));
}
if (strlen(pFormatCtx->genre) > 0)
{
log_debug("Added metadata genre: %s\n", pFormatCtx->genre);
item->setMetadata(String(MT_KEYS[M_GENRE].upnp),
sc->convert(String(pFormatCtx->genre)));
}
if (strlen(pFormatCtx->comment) > 0)
{
log_debug("Added metadata comment: %s\n", pFormatCtx->comment);
item->setMetadata(String(MT_KEYS[M_DESCRIPTION].upnp),
sc->convert(String(pFormatCtx->comment)));
}
if (pFormatCtx->track > 0)
{
log_debug("Added metadata track: %d\n", pFormatCtx->track);
item->setMetadata(String(MT_KEYS[M_TRACKNUMBER].upnp),
sc->convert(String::from(pFormatCtx->track)));
}
}
// ffmpeg library calls
static void addFfmpegResourceFields(Ref<CdsItem> item, AVFormatContext *pFormatCtx, int *x, int *y)
{
unsigned int i;
int hours, mins, secs, us;
int audioch = 0, samplefreq = 0;
bool audioset, videoset;
String resolution;
char duration[15];
// Initialize the buffers
duration[0] = 0;
*x = 0;
*y = 0;
// duration
secs = pFormatCtx->duration / AV_TIME_BASE;
us = pFormatCtx->duration % AV_TIME_BASE;
mins = secs / 60;
secs %= 60;
hours = mins / 60;
mins %= 60;
if ((hours + mins + secs) > 0)
{
sprintf(duration, "%02d:%02d:%02d.%01d", hours, mins,
secs, (10 * us) / AV_TIME_BASE);
log_debug("Added duration: %s\n", duration);
item->getResource(0)->addAttribute(MetadataHandler::getResAttrName(R_DURATION), String(duration));
}
// bitrate
if (pFormatCtx->bit_rate > 0)
{
log_debug("Added overall bitrate: %d kb/s\n",
pFormatCtx->bit_rate/1000);
item->getResource(0)->addAttribute(MetadataHandler::getResAttrName(R_BITRATE), String::from(pFormatCtx->bit_rate/1000));
}
// video resolution, audio sampling rate, nr of audio channels
audioset = false;
videoset = false;
for(i=0; i<pFormatCtx->nb_streams; i++)
{
AVStream *st = pFormatCtx->streams[i];
if((st != NULL) && (videoset == false) && (st->codec->codec_type == CODEC_TYPE_VIDEO))
{
if (st->codec->codec_tag > 0)
{
char fourcc[5];
fourcc[0] = st->codec->codec_tag;
fourcc[1] = st->codec->codec_tag >> 8;
fourcc[2] = st->codec->codec_tag >> 16;
fourcc[3] = st->codec->codec_tag >> 24;
fourcc[4] = '\0';
log_debug("FourCC: %x = %s\n",
st->codec->codec_tag, fourcc);
String fcc = String(fourcc);
if (string_ok(fcc))
item->getResource(0)->addOption(_(RESOURCE_OPTION_FOURCC),
fcc);
}
if ((st->codec->width > 0) && (st->codec->height > 0))
{
resolution = String::from(st->codec->width) + "x" +
String::from(st->codec->height);
log_debug("Added resolution: %s pixel\n", resolution.c_str());
item->getResource(0)->addAttribute(MetadataHandler::getResAttrName(R_RESOLUTION), resolution);
videoset = true;
*x = st->codec->width;
*y = st->codec->height;
}
}
if(st->codec->codec_type == CODEC_TYPE_AUDIO)
{
// Increase number of audiochannels
audioch++;
// Get the sample rate
if ((audioset == false) && (st->codec->sample_rate > 0))
{
samplefreq = st->codec->sample_rate;
if (samplefreq > 0)
{
log_debug("Added sample frequency: %d Hz\n", samplefreq);
item->getResource(0)->addAttribute(MetadataHandler::getResAttrName(R_SAMPLEFREQUENCY), String::from(samplefreq));
audioset = true;
}
}
}
}
if (audioch > 0)
{
log_debug("Added number of audio channels: %d\n", audioch);
item->getResource(0)->addAttribute(MetadataHandler::getResAttrName(R_NRAUDIOCHANNELS), String::from(audioch));
}
} // addFfmpegResourceFields
/*double time_to_double(struct timeval time) {
return time.tv_sec + (time.tv_usec / 1000000.0);
}*/
// Stub for suppressing ffmpeg error messages during matadata extraction
void FfmpegNoOutputStub(void* ptr, int level, const char* fmt, va_list vl)
{
// do nothing
}
void FfmpegHandler::fillMetadata(Ref<CdsItem> item)
{
log_debug("Running ffmpeg handler on %s\n", item->getLocation().c_str());
int x = 0;
int y = 0;
AVFormatContext *pFormatCtx;
// Suppress all log messages
av_log_set_callback(FfmpegNoOutputStub);
// Register all formats and codecs
av_register_all();
// Open video file
if (av_open_input_file(&pFormatCtx,
item->getLocation().c_str(), NULL, 0, NULL) != 0)
return; // Couldn't open file
// Retrieve stream information
if (av_find_stream_info(pFormatCtx) < 0)
{
av_close_input_file(pFormatCtx);
return; // Couldn't find stream information
}
// Add metadata using ffmpeg library calls
addFfmpegMetadataFields(item, pFormatCtx);
// Add resources using ffmpeg library calls
addFfmpegResourceFields(item, pFormatCtx, &x, &y);
// Close the video file
av_close_input_file(pFormatCtx);
}
Ref<IOHandler> FfmpegHandler::serveContent(Ref<CdsItem> item, int resNum, off_t *data_size)
{
*data_size = -1;
#ifdef HAVE_FFMPEGTHUMBNAILER
Ref<ConfigManager> cfg = ConfigManager::getInstance();
if (!cfg->getBoolOption(CFG_SERVER_EXTOPTS_FFMPEGTHUMBNAILER_ENABLED))
return nil;
#ifdef FFMPEGTHUMBNAILER_OLD_API
video_thumbnailer *th = create_thumbnailer();
image_data *img = create_image_data();
#else
video_thumbnailer *th = video_thumbnailer_create();
image_data *img = video_thumbnailer_create_image_data();
#endif // old api
th->seek_percentage = cfg->getIntOption(CFG_SERVER_EXTOPTS_FFMPEGTHUMBNAILER_SEEK_PERCENTAGE);
if (cfg->getBoolOption(CFG_SERVER_EXTOPTS_FFMPEGTHUMBNAILER_FILMSTRIP_OVERLAY))
th->overlay_film_strip = 1;
else
th->overlay_film_strip = 0;
th->thumbnail_size = cfg->getIntOption(CFG_SERVER_EXTOPTS_FFMPEGTHUMBNAILER_THUMBSIZE);
th->thumbnail_image_type = Jpeg;
log_debug("Generating thumbnail for file: %s\n", item->getLocation().c_str());
#ifdef FFMPEGTHUMBNAILER_OLD_API
if (generate_thumbnail_to_buffer(th, item->getLocation().c_str(), img) != 0)
#else
if (video_thumbnailer_generate_thumbnail_to_buffer(th,
item->getLocation().c_str(), img) != 0)
#endif // old api
throw _Exception(_("Could not generate thumbnail for ") +
item->getLocation());
*data_size = (off_t)img->image_data_size;
Ref<IOHandler> h(new MemIOHandler((void *)img->image_data_ptr,
img->image_data_size));
#ifdef FFMPEGTHUMBNAILER_OLD_API
destroy_image_data(img);
destroy_thumbnailer(th);
#else
video_thumbnailer_destroy_image_data(img);
video_thumbnailer_destroy(th);
#endif// old api
return h;
#else
return nil;
#endif
}
String FfmpegHandler::getMimeType()
{
Ref<ConfigManager> cfg = ConfigManager::getInstance();
Ref<Dictionary> mappings = cfg->getDictionaryOption(CFG_IMPORT_MAPPINGS_MIMETYPE_TO_CONTENTTYPE_LIST);
String thumb_mimetype = mappings->get(_(CONTENT_TYPE_JPG));
if (!string_ok(thumb_mimetype))
thumb_mimetype = _("image/jpeg");
return thumb_mimetype;
}
#endif // HAVE_FFMPEG
|