| 1 |
/* dvbcut |
| 2 |
Copyright (c) 2005 Sven Over <[email protected]> |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation; either version 2 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program; if not, write to the Free Software |
| 16 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 |
*/ |
| 18 |
|
| 19 |
/* $Id$ */ |
| 20 |
|
| 21 |
#include <qimage.h> |
| 22 |
#include <cstdlib> |
| 23 |
#include <cstdio> |
| 24 |
extern "C" { |
| 25 |
#include <libavutil/imgutils.h> |
| 26 |
} |
| 27 |
#include "avframe.h" |
| 28 |
|
| 29 |
#ifdef HAVE_LIB_SWSCALE |
| 30 |
avframe::avframe() : tobefreed(0),w(0),h(0),dw(0),pix_fmt(),img_convert_ctx(0) |
| 31 |
#else |
| 32 |
avframe::avframe() : tobefreed(0),w(0),h(0),dw(0),pix_fmt() |
| 33 |
#endif |
| 34 |
{ |
| 35 |
f=av_frame_alloc(); |
| 36 |
} |
| 37 |
|
| 38 |
avframe::avframe(AVFrame *src, AVCodecContext *ctx) : f(0),tobefreed(0) |
| 39 |
{ |
| 40 |
f=av_frame_alloc(); |
| 41 |
|
| 42 |
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 0, 0) |
| 43 |
tobefreed = (uint8_t *)malloc(av_image_get_buffer_size(ctx->pix_fmt, ctx->width, ctx->height, 1)); |
| 44 |
|
| 45 |
av_image_fill_arrays(f->data, f->linesize, |
| 46 |
tobefreed, |
| 47 |
ctx->pix_fmt, ctx->width, ctx->height, 1); |
| 48 |
|
| 49 |
av_image_copy(f->data, f->linesize, (const uint8_t**)src->data, src->linesize, |
| 50 |
ctx->pix_fmt, ctx->width, ctx->height); |
| 51 |
#else |
| 52 |
tobefreed = (uint8_t *)malloc(avpicture_get_size(ctx->pix_fmt, ctx->width, ctx->height)); |
| 53 |
|
| 54 |
avpicture_fill((AVPicture *)f, |
| 55 |
(u_int8_t*)tobefreed, |
| 56 |
ctx->pix_fmt,ctx->width,ctx->height); |
| 57 |
|
| 58 |
av_picture_copy((AVPicture *)f, (const AVPicture *) src, |
| 59 |
ctx->pix_fmt, ctx->width, ctx->height); |
| 60 |
#endif |
| 61 |
|
| 62 |
f->pict_type = src->pict_type; |
| 63 |
f->quality = src->quality; |
| 64 |
f->coded_picture_number = src->coded_picture_number; |
| 65 |
f->display_picture_number = src->display_picture_number; |
| 66 |
f->pts = src->pts; |
| 67 |
f->interlaced_frame = src->interlaced_frame; |
| 68 |
f->top_field_first = src->top_field_first; |
| 69 |
f->repeat_pict = src->repeat_pict; |
| 70 |
f->quality = src->quality; |
| 71 |
f->format = src->format; |
| 72 |
f->width = src->width; |
| 73 |
f->height = src->height; |
| 74 |
|
| 75 |
w=ctx->width; |
| 76 |
h=ctx->height; |
| 77 |
pix_fmt=ctx->pix_fmt; |
| 78 |
dw=w*ctx->sample_aspect_ratio.num/ctx->sample_aspect_ratio.den; |
| 79 |
#ifdef HAVE_LIB_SWSCALE |
| 80 |
img_convert_ctx=sws_getContext(w, h, pix_fmt, |
| 81 |
w, h, AV_PIX_FMT_RGB24, SWS_BICUBIC, |
| 82 |
NULL, NULL, NULL); |
| 83 |
#endif |
| 84 |
} |
| 85 |
|
| 86 |
avframe::~avframe() |
| 87 |
{ |
| 88 |
if (tobefreed) |
| 89 |
free(tobefreed); |
| 90 |
if (f) |
| 91 |
av_frame_free(&f); |
| 92 |
#ifdef HAVE_LIB_SWSCALE |
| 93 |
if (img_convert_ctx) |
| 94 |
sws_freeContext(img_convert_ctx); |
| 95 |
#endif |
| 96 |
} |
| 97 |
|
| 98 |
QImage avframe::getqimage(bool scaled, double viewscalefactor) |
| 99 |
{ |
| 100 |
#ifdef HAVE_LIB_SWSCALE |
| 101 |
if (w<=0 || h<=0 || img_convert_ctx==NULL) |
| 102 |
#else |
| 103 |
if (w<=0 || h<=0) |
| 104 |
#endif |
| 105 |
return QImage(); |
| 106 |
|
| 107 |
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 0, 0) |
| 108 |
uint8_t *rgbbuffer = (uint8_t*)malloc(av_image_get_buffer_size(AV_PIX_FMT_RGB24, w, h, 1)); |
| 109 |
#else |
| 110 |
uint8_t *rgbbuffer=(uint8_t*)malloc(avpicture_get_size(AV_PIX_FMT_RGB24, w, h)); |
| 111 |
#endif |
| 112 |
|
| 113 |
AVFrame *avframergb=av_frame_alloc(); |
| 114 |
|
| 115 |
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 0, 0) |
| 116 |
av_image_fill_arrays(avframergb->data, avframergb->linesize, |
| 117 |
rgbbuffer, |
| 118 |
AV_PIX_FMT_RGB24, w, h, 1); |
| 119 |
#else |
| 120 |
avpicture_fill((AVPicture*)avframergb, |
| 121 |
rgbbuffer, |
| 122 |
AV_PIX_FMT_RGB24,w,h); |
| 123 |
#endif |
| 124 |
|
| 125 |
#ifdef HAVE_LIB_SWSCALE |
| 126 |
sws_scale(img_convert_ctx, f->data, f->linesize, 0, h, |
| 127 |
avframergb->data, avframergb->linesize); |
| 128 |
#else |
| 129 |
img_convert((AVPicture *)avframergb, AV_PIX_FMT_RGB24, (AVPicture*)f, pix_fmt, w, h); |
| 130 |
#endif |
| 131 |
|
| 132 |
QImage im(rgbbuffer, w, h, 3*w, QImage::Format_RGB888, ::free, rgbbuffer); |
| 133 |
|
| 134 |
if ((scaled && w!=dw)||(viewscalefactor!=1.0)) { |
| 135 |
#ifdef SMOOTHSCALE |
| 136 |
im = im.scaled(int((scaled?dw:w)/viewscalefactor+0.5), int(h/viewscalefactor+0.5), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); |
| 137 |
#else |
| 138 |
im = im.scaled(int((scaled?dw:w)/viewscalefactor+0.5), int(h/viewscalefactor+0.5)); |
| 139 |
#endif |
| 140 |
} |
| 141 |
|
| 142 |
av_frame_free(&avframergb); |
| 143 |
return (im); |
| 144 |
} |