Change the read method of ffmpeg from filename to buffer to avoid
passing a filename with incorrect encoding to avformat_open_input. Increased the size of photo thumbnails a little.
This commit is contained in:
@@ -28,28 +28,70 @@
|
|||||||
AVDecoder::AvInit init;
|
AVDecoder::AvInit init;
|
||||||
|
|
||||||
AVDecoder::AVDecoder() :
|
AVDecoder::AVDecoder() :
|
||||||
pFormatCtx(NULL)
|
pFormatCtx(NULL), file(NULL)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
AVDecoder::~AVDecoder()
|
AVDecoder::~AVDecoder()
|
||||||
{
|
{
|
||||||
if(pFormatCtx) {
|
if(pFormatCtx) {
|
||||||
|
av_free(pFormatCtx->pb->buffer);
|
||||||
|
av_free(pFormatCtx->pb);
|
||||||
avformat_close_input(&pFormatCtx);
|
avformat_close_input(&pFormatCtx);
|
||||||
}
|
}
|
||||||
|
if(file) {
|
||||||
|
file->close();
|
||||||
|
delete file;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int AVDecoder::readFunction(void* opaque, uint8_t* buf, int buf_size)
|
||||||
|
{
|
||||||
|
QIODevice* stream = (QIODevice*)opaque;
|
||||||
|
int numBytes = stream->read((char*)buf, buf_size);
|
||||||
|
return numBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t AVDecoder::seekFunction(void* opaque, int64_t offset, int whence)
|
||||||
|
{
|
||||||
|
if (whence == AVSEEK_SIZE) {
|
||||||
|
return -1; // I don't know "size of my handle in bytes"
|
||||||
|
}
|
||||||
|
QIODevice* stream = (QIODevice*)opaque;
|
||||||
|
if (stream->isSequential()) {
|
||||||
|
return -1; // cannot seek a sequential stream
|
||||||
|
}
|
||||||
|
if (! stream->seek(offset) ) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return stream->pos();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AVDecoder::open(const QString filename)
|
bool AVDecoder::open(const QString filename)
|
||||||
{
|
{
|
||||||
if(avformat_open_input(&pFormatCtx, QFile::encodeName(filename).constData(), NULL, NULL) != 0) {
|
file = new QFile(filename);
|
||||||
|
if(!file->open(QIODevice::ReadOnly)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const int ioBufferSize = 32768;
|
||||||
|
unsigned char *ioBuffer = (unsigned char *)av_malloc(ioBufferSize + FF_INPUT_BUFFER_PADDING_SIZE);
|
||||||
|
AVIOContext *avioContext = avio_alloc_context(ioBuffer, ioBufferSize, 0, (void*)(file), &AVDecoder::readFunction, NULL, &AVDecoder::seekFunction);
|
||||||
|
pFormatCtx = avformat_alloc_context();
|
||||||
|
pFormatCtx->pb = avioContext;
|
||||||
|
|
||||||
|
|
||||||
|
if(avformat_open_input(&pFormatCtx, "dummy", NULL, NULL) != 0) {
|
||||||
|
av_free(avioContext->buffer);
|
||||||
|
av_free(avioContext);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(avformat_find_stream_info(pFormatCtx, NULL) < 0) {
|
if(avformat_find_stream_info(pFormatCtx, NULL) < 0) {
|
||||||
|
av_free(avioContext->buffer);
|
||||||
|
av_free(avioContext);
|
||||||
avformat_close_input(&pFormatCtx);
|
avformat_close_input(&pFormatCtx);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,6 +315,10 @@ QByteArray AVDecoder::getVideoThumbnail(int width, int height)
|
|||||||
|
|
||||||
void AVDecoder::close()
|
void AVDecoder::close()
|
||||||
{
|
{
|
||||||
|
av_free(pFormatCtx->pb->buffer);
|
||||||
|
av_free(pFormatCtx->pb);
|
||||||
avformat_close_input(&pFormatCtx);
|
avformat_close_input(&pFormatCtx);
|
||||||
pFormatCtx = NULL;
|
pFormatCtx = NULL;
|
||||||
|
file->close();
|
||||||
|
file = NULL;
|
||||||
}
|
}
|
||||||
|
@@ -20,6 +20,7 @@
|
|||||||
#ifndef AVDECODER_H
|
#ifndef AVDECODER_H
|
||||||
#define AVDECODER_H
|
#define AVDECODER_H
|
||||||
|
|
||||||
|
#include <QFile>
|
||||||
#include <QImage>
|
#include <QImage>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
@@ -62,7 +63,11 @@ private:
|
|||||||
void AVFrameToQImage(AVFrame &frame, QImage &image, int width, int height);
|
void AVFrameToQImage(AVFrame &frame, QImage &image, int width, int height);
|
||||||
AVFrame *getDecodedFrame(AVCodecContext *pCodecCtx, int stream_index);
|
AVFrame *getDecodedFrame(AVCodecContext *pCodecCtx, int stream_index);
|
||||||
|
|
||||||
|
static int readFunction(void* opaque, uint8_t* buf, int buf_size);
|
||||||
|
static int64_t seekFunction(void* opaque, int64_t offset, int whence);
|
||||||
|
|
||||||
AVFormatContext *pFormatCtx;
|
AVFormatContext *pFormatCtx;
|
||||||
|
QFile *file;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // AVDECODER_H
|
#endif // AVDECODER_H
|
||||||
|
@@ -127,7 +127,7 @@ QByteArray getThumbnail(const QString &path, DataType type, metadata_t *metadata
|
|||||||
if(img.load(path)) {
|
if(img.load(path)) {
|
||||||
QBuffer buffer(&data);
|
QBuffer buffer(&data);
|
||||||
buffer.open(QIODevice::WriteOnly);
|
buffer.open(QIODevice::WriteOnly);
|
||||||
QImage result = img.scaled(213, 120, Qt::KeepAspectRatio, Qt::FastTransformation);
|
QImage result = img.scaled(240, 136, Qt::KeepAspectRatio, Qt::FastTransformation);
|
||||||
result.save(&buffer, "JPEG");
|
result.save(&buffer, "JPEG");
|
||||||
metadata->data.thumbnail.width = result.width();
|
metadata->data.thumbnail.width = result.width();
|
||||||
metadata->data.thumbnail.height = result.height();
|
metadata->data.thumbnail.height = result.height();
|
||||||
|
Reference in New Issue
Block a user