
Recherche avancée
Autres articles (46)
-
Des sites réalisés avec MediaSPIP
2 mai 2011, parCette page présente quelques-uns des sites fonctionnant sous MediaSPIP.
Vous pouvez bien entendu ajouter le votre grâce au formulaire en bas de page. -
Librairies et binaires spécifiques au traitement vidéo et sonore
31 janvier 2010, parLes logiciels et librairies suivantes sont utilisées par SPIPmotion d’une manière ou d’une autre.
Binaires obligatoires FFMpeg : encodeur principal, permet de transcoder presque tous les types de fichiers vidéo et sonores dans les formats lisibles sur Internet. CF ce tutoriel pour son installation ; Oggz-tools : outils d’inspection de fichiers ogg ; Mediainfo : récupération d’informations depuis la plupart des formats vidéos et sonores ;
Binaires complémentaires et facultatifs flvtool2 : (...) -
Changer son thème graphique
22 février 2011, parLe thème graphique ne touche pas à la disposition à proprement dite des éléments dans la page. Il ne fait que modifier l’apparence des éléments.
Le placement peut être modifié effectivement, mais cette modification n’est que visuelle et non pas au niveau de la représentation sémantique de la page.
Modifier le thème graphique utilisé
Pour modifier le thème graphique utilisé, il est nécessaire que le plugin zen-garden soit activé sur le site.
Il suffit ensuite de se rendre dans l’espace de configuration du (...)
Sur d’autres sites (6332)
-
Can't decode h264 frame by frame
17 mai 2017, par J. DoeHere is problematic code :
int frame_count{0};
int status = -1;
while ((status = av_read_frame(ctx, pkt)) >= 0) {
int got_frame;
auto len =
avcodec_decode_video2(video_ctx, frame, &got_frame, pkt);
errcheck(len);
if (got_frame == 0)
errthrow("No frame could be decompressed");
auto w = frame->width;
auto h = frame->height;
auto gray_convert_ctx = sws_getContext(
w, h, input_pix_format, w, h, output_pix_format, SWS_POINT,
nullptr, nullptr, nullptr);
sws_scale(gray_convert_ctx, frame->data, frame->linesize, 0, h,
frame_converted->data, frame_converted->linesize);
f_(frame_converted->data[0], frame_converted->linesize[0], w,
h);
++frame_count;
sws_freeContext(gray_convert_ctx);
if (pkt->data) {
pkt->size -= len;
pkt->data += len;
}
}
if (status != AVERROR_EOF)
errcheck(status);With
vp8/vp9
all is okay, but when I’m trying to decodeh264
, I’ve got error :➤ ./cv /tmp/x
file size: 694KiB
read /tmp/x: 694KiB
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x191fc00] Format mov,mp4,m4a,3gp,3g2,mj2 probed with size=2048 and score=100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x191fc00] ISO: File Type Major Brand: mp42
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x191fc00] rfps: 31.000000 0.000599
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x191fc00] Before avformat_find_stream_info() pos: 3104 bytes read:32768 seeks:0
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x191fc00] All info found
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x191fc00] After avformat_find_stream_info() pos: 456697 bytes read:458752 seeks:0 frames:34
[h264 @ 0x1926700] no frame!
error: AV: Invalid data found when processing inputMaybe that’s because of h264 does not support AV_CODEC_CAP_TRUNCATED ? But I suppose it’s should be handled in av_read_frame.
Then ignoring (just skipping this read if error occurs) — no frame decoded anyway until EOF. :c This video working fine with ffplay/mpv/etc. and was recorded by Android.
#include <iostream>
extern "C" {
#include <libavcodec></libavcodec>avcodec.h>
#include <libavformat></libavformat>avformat.h>
}
#include <functional>
class Video
{
public:
static std::string TAG;
Video();
Video(void *data_ptr, size_t data_size);
~Video();
void set(void *data_ptr, size_t data_size);
void process(std::function f_);
private:
static constexpr AVPixelFormat output_pix_format{AV_PIX_FMT_GRAY8};
struct {
uint8_t *ptr{nullptr};
size_t size;
} bd;
bool video_ctx_opened{false};
AVCodecContext *video_ctx{nullptr};
AVStream *video_stream{nullptr};
size_t width;
size_t heigh;
AVPixelFormat input_pix_format;
size_t avio_ctx_buffer_size = 32 * 1024; // 32 KiB
uint8_t *avio_ctx_buffer{nullptr};
AVFormatContext *ctx{nullptr};
AVIOContext *avio_ctx{nullptr};
uint8_t *frame_converted_buffer{nullptr};
AVFrame *frame_converted{nullptr};
AVFrame *frame{nullptr};
AVPacket *pkt{nullptr};
void init_stream();
void init_codec();
void init_frame_converted();
};
extern "C" {
#include <libswscale></libswscale>swscale.h>
}
#include <iostream>
#include <stdexcept>
namespace
{
using str_t = decltype(Video::TAG);
static str_t averr(int code)
{
static thread_local std::array buf;
av_make_error_string(buf.data(), buf.size(), code);
return str_t(buf.data(), buf.size());
}
static str_t errstr(int err) { return Video::TAG + ": " + averr(err); }
static str_t errstr(const char *err) { return Video::TAG + ": " + err; }
static void errthrow(str_t err) { throw std::runtime_error{std::move(err)}; }
static void errcheck(int val)
{
if (val < 0)
errthrow(errstr(val));
}
template <class t="t"> static void errcheck(T *ptr, const char *errmsg)
{
if (!ptr)
errthrow(errstr(errmsg));
}
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
{
struct _bd {
uint8_t *ptr;
size_t size;
};
_bd *bd = static_cast<_bd *>(opaque);
buf_size = FFMIN(buf_size, bd->size);
memcpy(buf, bd->ptr, buf_size);
bd->ptr += buf_size;
bd->size -= buf_size;
return buf_size;
}
}
std::string Video::TAG = "AV";
Video::Video()
{
av_register_all();
avcodec_register_all();
frame = av_frame_alloc();
errcheck(frame, "Could not allocate frame");
pkt = static_cast<avpacket>(av_malloc(sizeof(AVPacket)));
errcheck(pkt, "Could not allocate packet");
av_init_packet(pkt);
}
Video::Video(void *data_ptr, size_t data_size) : Video()
{
set(data_ptr, data_size);
}
Video::~Video()
{
avformat_close_input(&ctx);
if (avio_ctx) {
av_freep(&avio_ctx->buffer);
av_freep(&avio_ctx);
}
if (video_ctx) {
avcodec_close(video_ctx);
av_free(video_ctx);
}
if (frame)
av_frame_free(&frame);
if (frame_converted_buffer)
av_freep(&frame_converted_buffer);
if (frame_converted)
av_frame_free(&frame_converted);
if (pkt) {
av_free_packet(pkt);
av_free(pkt);
}
}
void Video::set(void *data_ptr, size_t data_size)
{
bd.ptr = static_cast(data_ptr);
bd.size = data_size;
init_stream();
init_frame_converted();
init_codec();
pkt->data = nullptr;
pkt->size = 0;
}
void Video::process(
std::function f_)
{
int frame_count{0};
int status = -1;
while ((status = av_read_frame(ctx, pkt)) >= 0) {
int got_frame;
auto len =
avcodec_decode_video2(video_ctx, frame, &got_frame, pkt);
errcheck(len);
if (got_frame == 0)
errthrow("No frame could be decompressed");
auto w = frame->width;
auto h = frame->height;
auto gray_convert_ctx = sws_getContext(
w, h, input_pix_format, w, h, output_pix_format, SWS_POINT,
nullptr, nullptr, nullptr);
sws_scale(gray_convert_ctx, frame->data, frame->linesize, 0, h,
frame_converted->data, frame_converted->linesize);
f_(frame_converted->data[0], frame_converted->linesize[0], w,
h);
++frame_count;
sws_freeContext(gray_convert_ctx);
if (pkt->data) {
pkt->size -= len;
pkt->data += len;
}
}
if (status != AVERROR_EOF)
errcheck(status);
}
void Video::init_stream()
{
ctx = avformat_alloc_context();
errcheck(ctx, "Could not allocate format context");
avio_ctx_buffer =
static_cast(av_malloc(avio_ctx_buffer_size));
errcheck(avio_ctx_buffer, "Could not allocate io buffer");
avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0,
&bd, &read_packet, nullptr, nullptr);
errcheck(avio_ctx, "Could not allocate io context");
ctx->pb = avio_ctx;
auto status = avformat_open_input(&ctx, nullptr, nullptr, nullptr);
errcheck(status);
status = avformat_find_stream_info(ctx, nullptr);
errcheck(status);
for (decltype(ctx->nb_streams) i = 0; i < ctx->nb_streams; ++i) {
auto stream = ctx->streams[i];
if (!stream || !stream->codec)
continue;
if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
video_stream = stream;
break;
}
}
errcheck(video_stream, "Could not find valid video stream");
width = video_stream->codec->width;
heigh = video_stream->codec->height;
input_pix_format = video_stream->codec->pix_fmt;
}
void Video::init_codec()
{
auto codec = avcodec_find_decoder(video_stream->codec->codec_id);
errcheck(codec, "Codec not found");
video_ctx = avcodec_alloc_context3(codec);
errcheck(video_ctx, "Could not allocate video codec context");
if (codec->capabilities & AV_CODEC_CAP_TRUNCATED)
video_ctx->flags |=
AV_CODEC_FLAG_TRUNCATED; // we do not send complete frames
auto status = avcodec_open2(video_ctx, codec, nullptr);
errcheck(status);
}
void Video::init_frame_converted()
{
frame_converted = av_frame_alloc();
errcheck(frame_converted, "Could not allocate frame");
int frame_converted_buffer_size =
avpicture_get_size(output_pix_format, width, heigh);
errcheck(frame_converted_buffer_size);
frame_converted_buffer =
static_cast(av_malloc(frame_converted_buffer_size));
errcheck(frame_converted_buffer, "Could not allocate picture buffer");
auto status = avpicture_fill(
reinterpret_cast<avpicture>(frame_converted),
frame_converted_buffer, output_pix_format, width, heigh);
errcheck(status);
}
#include <vector>
#include <fstream>
std::vector<char> read_file(const std::string &fname)
{
std::ifstream file(fname, std::ios::binary | std::ios::ate);
if (!file.is_open())
throw std::runtime_error{"can't open " + fname};
auto size = file.tellg();
file.seekg(0, std::ios::beg);
std::cout << "file size: " << std::to_string(size / 1024) << "KiB\n";
std::vector<char> buffer(size);
if (file.read(buffer.data(), size))
return buffer;
return {};
}
int main(int argc, const char **argv)
{
if (argc < 2)
return EXIT_FAILURE;
av_log_set_level(AV_LOG_DEBUG);
try {
auto data = read_file(argv[1]);
std::cout << "read " << argv[1] << ": "
<< std::to_string(data.size() / 1024) << "KiB\n";
Video v;
v.set(data.data(), data.size());
v.process([](unsigned char *data, int wrap, int xsize,
int ysize) {
std::cout << "w: " << xsize
<< " h: " << ysize << '\n';
});
} catch (const std::runtime_error &e) {
std::cout << "error: " << e.what() << '\n';
}
}
</char></char></fstream></vector></avpicture></avpacket></class></stdexcept></iostream></functional></iostream>Compile&Run :
g++ cv.cpp -std=c++14 -lavutil -lavcodec -lavformat -lswscale ; ./a.out file.name.here
-
What Is Ethical SEO & Why Does It Matter ?
7 mai 2024, par Erin -
Unwrapping Matomo 5.2.0 – Bringing you enhanced security and performance
25 décembre 2024, par Daniel Crough — Latest Releases