mirror of https://github.com/FFmpeg/FFmpeg.git
* cigaes/master: lavf/lavd: version bump and APIchanges for uncoded frames. tools: add uncoded_frame test program. lavf: add uncodedframecrc test muxer. lavd/xv: preliminary support of uncoded frame. lavd/alsa: preliminary support of uncoded frame. lavf: add write_uncoded_frame() API. Merged-by: Michael Niedermayer <michaelni@gmx.at>pull/43/merge
commit
058a3d6542
13 changed files with 640 additions and 13 deletions
@ -0,0 +1,172 @@ |
||||
/*
|
||||
* Copyright (c) 2013 Nicolas George |
||||
* |
||||
* This file is part of FFmpeg. |
||||
* |
||||
* FFmpeg is free software; you can redistribute it and/or |
||||
* modify it under the terms of the GNU Lesser General Public License |
||||
* as published by the Free Software Foundation; either |
||||
* version 2.1 of the License, or (at your option) any later version. |
||||
* |
||||
* FFmpeg 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 Lesser General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU Lesser General Public License |
||||
* along with FFmpeg; if not, write to the Free Software Foundation, Inc., |
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
||||
*/ |
||||
|
||||
#include "libavutil/adler32.h" |
||||
#include "libavutil/avassert.h" |
||||
#include "libavutil/bprint.h" |
||||
#include "libavutil/imgutils.h" |
||||
#include "libavutil/pixdesc.h" |
||||
#include "avformat.h" |
||||
#include "internal.h" |
||||
|
||||
/* Identical to Adler32 when the type is uint8_t. */ |
||||
#define DEFINE_CKSUM_LINE(name, type, conv) \ |
||||
static void cksum_line_ ## name(unsigned *cksum, void *data, unsigned size) \
|
||||
{ \
|
||||
type *p = data; \
|
||||
unsigned a = *cksum & 0xFFFF, b = *cksum >> 16; \
|
||||
for (; size > 0; size--, p++) { \
|
||||
a = (a + (unsigned)(conv)) % 65521; \
|
||||
b = (b + a) % 65521; \
|
||||
} \
|
||||
*cksum = a | (b << 16); \
|
||||
} |
||||
|
||||
DEFINE_CKSUM_LINE(u8, uint8_t, *p) |
||||
DEFINE_CKSUM_LINE(s16, int16_t, *p + 0x8000) |
||||
DEFINE_CKSUM_LINE(s32, int32_t, *p + 0x80000000) |
||||
DEFINE_CKSUM_LINE(flt, float, *p * 0x80000000 + 0x80000000) |
||||
DEFINE_CKSUM_LINE(dbl, double, *p * 0x80000000 + 0x80000000) |
||||
|
||||
static void video_frame_cksum(AVBPrint *bp, AVFrame *frame) |
||||
{ |
||||
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format); |
||||
int i, y; |
||||
uint8_t *data; |
||||
int linesize[5] = { 0 }; |
||||
|
||||
av_bprintf(bp, ", %d x %d", frame->width, frame->height); |
||||
if (!desc) { |
||||
av_bprintf(bp, ", unknown"); |
||||
return; |
||||
} |
||||
if (av_image_fill_linesizes(linesize, frame->format, frame->width) < 0) |
||||
return; |
||||
av_bprintf(bp, ", %s", desc->name); |
||||
for (i = 0; linesize[i]; i++) { |
||||
unsigned cksum = 0; |
||||
int h = frame->height; |
||||
if ((i == 1 || i == 2) && desc->nb_components >= 3) |
||||
h = -((-h) >> desc->log2_chroma_h); |
||||
data = frame->data[i]; |
||||
for (y = 0; y < h; y++) { |
||||
cksum = av_adler32_update(cksum, data, linesize[i]); |
||||
data += frame->linesize[i]; |
||||
} |
||||
av_bprintf(bp, ", 0x%08x", cksum); |
||||
} |
||||
} |
||||
|
||||
static void audio_frame_cksum(AVBPrint *bp, AVFrame *frame) |
||||
{ |
||||
int nb_planes, nb_samples, p; |
||||
const char *name; |
||||
|
||||
nb_planes = av_frame_get_channels(frame); |
||||
nb_samples = frame->nb_samples; |
||||
if (!av_sample_fmt_is_planar(frame->format)) { |
||||
nb_samples *= nb_planes; |
||||
nb_planes = 1; |
||||
} |
||||
name = av_get_sample_fmt_name(frame->format); |
||||
av_bprintf(bp, ", %d samples", frame->nb_samples); |
||||
av_bprintf(bp, ", %s", name ? name : "unknown"); |
||||
for (p = 0; p < nb_planes; p++) { |
||||
uint32_t cksum = 0; |
||||
void *d = frame->extended_data[p]; |
||||
switch (frame->format) { |
||||
case AV_SAMPLE_FMT_U8: |
||||
case AV_SAMPLE_FMT_U8P: |
||||
cksum_line_u8(&cksum, d, nb_samples); |
||||
break; |
||||
case AV_SAMPLE_FMT_S16: |
||||
case AV_SAMPLE_FMT_S16P: |
||||
cksum_line_s16(&cksum, d, nb_samples); |
||||
break; |
||||
case AV_SAMPLE_FMT_S32: |
||||
case AV_SAMPLE_FMT_S32P: |
||||
cksum_line_s32(&cksum, d, nb_samples); |
||||
break; |
||||
case AV_SAMPLE_FMT_FLT: |
||||
case AV_SAMPLE_FMT_FLTP: |
||||
cksum_line_flt(&cksum, d, nb_samples); |
||||
break; |
||||
case AV_SAMPLE_FMT_DBL: |
||||
case AV_SAMPLE_FMT_DBLP: |
||||
cksum_line_dbl(&cksum, d, nb_samples); |
||||
break; |
||||
default: |
||||
av_assert0(!"reached"); |
||||
} |
||||
av_bprintf(bp, ", 0x%08x", cksum); |
||||
} |
||||
} |
||||
|
||||
static int write_frame(struct AVFormatContext *s, int stream_index, |
||||
AVFrame **frame, unsigned flags) |
||||
{ |
||||
AVBPrint bp; |
||||
int ret = 0; |
||||
enum AVMediaType type; |
||||
const char *type_name; |
||||
|
||||
if ((flags & AV_WRITE_UNCODED_FRAME_QUERY)) |
||||
return 0; |
||||
|
||||
av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED); |
||||
av_bprintf(&bp, "%d, %10"PRId64"", |
||||
stream_index, (*frame)->pts); |
||||
type = s->streams[stream_index]->codec->codec_type; |
||||
type_name = av_get_media_type_string(type); |
||||
av_bprintf(&bp, ", %s", type_name ? type_name : "unknown"); |
||||
switch (type) { |
||||
case AVMEDIA_TYPE_VIDEO: |
||||
video_frame_cksum(&bp, *frame); |
||||
break; |
||||
case AVMEDIA_TYPE_AUDIO: |
||||
audio_frame_cksum(&bp, *frame); |
||||
break; |
||||
} |
||||
|
||||
av_bprint_chars(&bp, '\n', 1); |
||||
if (av_bprint_is_complete(&bp)) |
||||
avio_write(s->pb, bp.str, bp.len); |
||||
else |
||||
ret = AVERROR(ENOMEM); |
||||
av_bprint_finalize(&bp, NULL); |
||||
return ret; |
||||
} |
||||
|
||||
static int write_packet(struct AVFormatContext *s, AVPacket *pkt) |
||||
{ |
||||
return AVERROR(ENOSYS); |
||||
} |
||||
|
||||
AVOutputFormat ff_uncodedframecrc_muxer = { |
||||
.name = "uncodedframecrc", |
||||
.long_name = NULL_IF_CONFIG_SMALL("uncoded framecrc testing"), |
||||
.audio_codec = AV_CODEC_ID_PCM_S16LE, |
||||
.video_codec = AV_CODEC_ID_RAWVIDEO, |
||||
.write_header = ff_framehash_write_header, |
||||
.write_packet = write_packet, |
||||
.write_uncoded_frame = write_frame, |
||||
.flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT | |
||||
AVFMT_TS_NEGATIVE, |
||||
}; |
@ -0,0 +1,279 @@ |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include "libavutil/avassert.h" |
||||
#include "libavdevice/avdevice.h" |
||||
#include "libavfilter/avfilter.h" |
||||
#include "libavfilter/buffersink.h" |
||||
#include "libavformat/avformat.h" |
||||
|
||||
typedef struct { |
||||
AVFormatContext *mux; |
||||
AVStream *stream; |
||||
AVFilterContext *sink; |
||||
AVFilterLink *link; |
||||
} Stream; |
||||
|
||||
static int create_sink(Stream *st, AVFilterGraph *graph, |
||||
AVFilterContext *f, int idx) |
||||
{ |
||||
enum AVMediaType type = avfilter_pad_get_type(f->output_pads, idx); |
||||
const char *sink_name; |
||||
int ret; |
||||
|
||||
switch (type) { |
||||
case AVMEDIA_TYPE_VIDEO: sink_name = "buffersink"; break; |
||||
case AVMEDIA_TYPE_AUDIO: sink_name = "abuffersink"; break; |
||||
default: |
||||
av_log(NULL, AV_LOG_ERROR, "Stream type not supported\n"); |
||||
return AVERROR(EINVAL); |
||||
} |
||||
ret = avfilter_graph_create_filter(&st->sink, |
||||
avfilter_get_by_name(sink_name), |
||||
NULL, NULL, NULL, graph); |
||||
if (ret < 0) |
||||
return ret; |
||||
ret = avfilter_link(f, idx, st->sink, 0); |
||||
if (ret < 0) |
||||
return ret; |
||||
st->link = st->sink->inputs[0]; |
||||
return 0; |
||||
} |
||||
|
||||
int main(int argc, char **argv) |
||||
{ |
||||
char *in_graph_desc, **out_dev_name; |
||||
int nb_out_dev = 0, nb_streams = 0; |
||||
AVFilterGraph *in_graph = NULL; |
||||
Stream *streams = NULL, *st; |
||||
AVFrame *frame = NULL; |
||||
int i, j, run = 1, ret; |
||||
|
||||
//av_log_set_level(AV_LOG_DEBUG);
|
||||
|
||||
if (argc < 3) { |
||||
av_log(NULL, AV_LOG_ERROR, |
||||
"Usage: %s filter_graph dev:out [dev2:out2...]\n\n" |
||||
"Examples:\n" |
||||
"%s movie=file.nut:s=v+a xv:- alsa:default\n" |
||||
"%s movie=file.nut:s=v+a uncodedframecrc:pipe:0\n", |
||||
argv[0], argv[0], argv[0]); |
||||
exit(1); |
||||
} |
||||
in_graph_desc = argv[1]; |
||||
out_dev_name = argv + 2; |
||||
nb_out_dev = argc - 2; |
||||
|
||||
av_register_all(); |
||||
avdevice_register_all(); |
||||
avfilter_register_all(); |
||||
|
||||
/* Create input graph */ |
||||
if (!(in_graph = avfilter_graph_alloc())) { |
||||
ret = AVERROR(ENOMEM); |
||||
av_log(NULL, AV_LOG_ERROR, "Unable to alloc graph graph: %s\n", |
||||
av_err2str(ret)); |
||||
goto fail; |
||||
} |
||||
ret = avfilter_graph_parse_ptr(in_graph, in_graph_desc, NULL, NULL, NULL); |
||||
if (ret < 0) { |
||||
av_log(NULL, AV_LOG_ERROR, "Unable to parse graph: %s\n", |
||||
av_err2str(ret)); |
||||
goto fail; |
||||
} |
||||
nb_streams = 0; |
||||
for (i = 0; i < in_graph->nb_filters; i++) { |
||||
AVFilterContext *f = in_graph->filters[i]; |
||||
for (j = 0; j < f->nb_inputs; j++) { |
||||
if (!f->inputs[j]) { |
||||
av_log(NULL, AV_LOG_ERROR, "Graph has unconnected inputs\n"); |
||||
ret = AVERROR(EINVAL); |
||||
goto fail; |
||||
} |
||||
} |
||||
for (j = 0; j < f->nb_outputs; j++) |
||||
if (!f->outputs[j]) |
||||
nb_streams++; |
||||
} |
||||
if (!nb_streams) { |
||||
av_log(NULL, AV_LOG_ERROR, "Graph has no output stream\n"); |
||||
ret = AVERROR(EINVAL); |
||||
goto fail; |
||||
} |
||||
if (nb_out_dev != 1 && nb_out_dev != nb_streams) { |
||||
av_log(NULL, AV_LOG_ERROR, |
||||
"Graph has %d output streams, %d devices given\n", |
||||
nb_streams, nb_out_dev); |
||||
ret = AVERROR(EINVAL); |
||||
goto fail; |
||||
} |
||||
|
||||
if (!(streams = av_calloc(nb_streams, sizeof(*streams)))) { |
||||
ret = AVERROR(ENOMEM); |
||||
av_log(NULL, AV_LOG_ERROR, "Could not allocate streams\n"); |
||||
} |
||||
st = streams; |
||||
for (i = 0; i < in_graph->nb_filters; i++) { |
||||
AVFilterContext *f = in_graph->filters[i]; |
||||
for (j = 0; j < f->nb_outputs; j++) { |
||||
if (!f->outputs[j]) { |
||||
if ((ret = create_sink(st++, in_graph, f, j)) < 0) |
||||
goto fail; |
||||
} |
||||
} |
||||
} |
||||
av_assert0(st - streams == nb_streams); |
||||
if ((ret = avfilter_graph_config(in_graph, NULL)) < 0) { |
||||
av_log(NULL, AV_LOG_ERROR, "Failed to configure graph\n"); |
||||
goto fail; |
||||
} |
||||
|
||||
/* Create output devices */ |
||||
for (i = 0; i < nb_out_dev; i++) { |
||||
char *fmt = NULL, *dev = out_dev_name[i]; |
||||
st = &streams[i]; |
||||
if ((dev = strchr(dev, ':'))) { |
||||
*(dev++) = 0; |
||||
fmt = out_dev_name[i]; |
||||
} |
||||
ret = avformat_alloc_output_context2(&st->mux, NULL, fmt, dev); |
||||
if (ret < 0) { |
||||
av_log(NULL, AV_LOG_ERROR, "Failed to allocate output: %s\n", |
||||
av_err2str(ret)); |
||||
goto fail; |
||||
} |
||||
if (!(st->mux->oformat->flags & AVFMT_NOFILE)) { |
||||
ret = avio_open2(&st->mux->pb, st->mux->filename, AVIO_FLAG_WRITE, |
||||
NULL, NULL); |
||||
if (ret < 0) { |
||||
av_log(st->mux, AV_LOG_ERROR, "Failed to init output: %s\n", |
||||
av_err2str(ret)); |
||||
goto fail; |
||||
} |
||||
} |
||||
} |
||||
for (; i < nb_streams; i++) |
||||
streams[i].mux = streams[0].mux; |
||||
|
||||
/* Create output device streams */ |
||||
for (i = 0; i < nb_streams; i++) { |
||||
st = &streams[i]; |
||||
if (!(st->stream = avformat_new_stream(st->mux, NULL))) { |
||||
ret = AVERROR(ENOMEM); |
||||
av_log(NULL, AV_LOG_ERROR, "Failed to create output stream\n"); |
||||
goto fail; |
||||
} |
||||
st->stream->codec->codec_type = st->link->type; |
||||
st->stream->time_base = st->stream->codec->time_base = |
||||
st->link->time_base; |
||||
switch (st->link->type) { |
||||
case AVMEDIA_TYPE_VIDEO: |
||||
st->stream->codec->codec_id = AV_CODEC_ID_RAWVIDEO; |
||||
st->stream->avg_frame_rate = |
||||
st->stream-> r_frame_rate = av_buffersink_get_frame_rate(st->sink); |
||||
st->stream->codec->width = st->link->w; |
||||
st->stream->codec->height = st->link->h; |
||||
st->stream->codec->sample_aspect_ratio = st->link->sample_aspect_ratio; |
||||
st->stream->codec->pix_fmt = st->link->format; |
||||
break; |
||||
case AVMEDIA_TYPE_AUDIO: |
||||
st->stream->codec->codec_id = |
||||
av_get_pcm_codec(st->stream->codec->sample_fmt, -1); |
||||
st->stream->codec->channel_layout = st->link->channel_layout; |
||||
st->stream->codec->channels = avfilter_link_get_channels(st->link); |
||||
st->stream->codec->sample_rate = st->link->sample_rate; |
||||
st->stream->codec->sample_fmt = st->link->format;
|
||||
break; |
||||
default: |
||||
av_assert0(!"reached"); |
||||
} |
||||
} |
||||
|
||||
/* Init output devices */ |
||||
for (i = 0; i < nb_out_dev; i++) { |
||||
st = &streams[i]; |
||||
if ((ret = avformat_write_header(st->mux, NULL)) < 0) { |
||||
av_log(st->mux, AV_LOG_ERROR, "Failed to init output: %s\n", |
||||
av_err2str(ret)); |
||||
goto fail; |
||||
} |
||||
} |
||||
|
||||
/* Check output devices */ |
||||
for (i = 0; i < nb_streams; i++) { |
||||
st = &streams[i]; |
||||
ret = av_write_uncoded_frame_query(st->mux, st->stream->index); |
||||
if (ret < 0) { |
||||
av_log(st->mux, AV_LOG_ERROR, |
||||
"Uncoded frames not supported on stream #%d: %s\n", |
||||
i, av_err2str(ret)); |
||||
goto fail; |
||||
} |
||||
} |
||||
|
||||
while (run) { |
||||
ret = avfilter_graph_request_oldest(in_graph); |
||||
if (ret < 0) { |
||||
if (ret == AVERROR_EOF) { |
||||
run = 0; |
||||
} else { |
||||
av_log(NULL, AV_LOG_ERROR, "Error filtering: %s\n", |
||||
av_err2str(ret)); |
||||
break; |
||||
} |
||||
} |
||||
for (i = 0; i < nb_streams; i++) { |
||||
st = &streams[i]; |
||||
while (1) { |
||||
if (!frame && !(frame = av_frame_alloc())) { |
||||
ret = AVERROR(ENOMEM); |
||||
av_log(NULL, AV_LOG_ERROR, "Could not allocate frame\n"); |
||||
goto fail; |
||||
} |
||||
ret = av_buffersink_get_frame_flags(st->sink, frame, |
||||
AV_BUFFERSINK_FLAG_NO_REQUEST); |
||||
if (ret < 0) { |
||||
if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) |
||||
av_log(NULL, AV_LOG_WARNING, "Error in sink: %s\n", |
||||
av_err2str(ret)); |
||||
break; |
||||
} |
||||
if (frame->pts != AV_NOPTS_VALUE) |
||||
frame->pts = av_rescale_q(frame->pts, |
||||
st->link ->time_base, |
||||
st->stream->time_base); |
||||
ret = av_interleaved_write_uncoded_frame(st->mux, |
||||
st->stream->index, |
||||
frame); |
||||
if (ret < 0) { |
||||
av_log(st->stream->codec, AV_LOG_ERROR, |
||||
"Error writing frame: %s\n", av_err2str(ret)); |
||||
goto fail; |
||||
} |
||||
frame = NULL; |
||||
} |
||||
} |
||||
} |
||||
ret = 0; |
||||
|
||||
for (i = 0; i < nb_out_dev; i++) { |
||||
st = &streams[i]; |
||||
av_write_trailer(st->mux); |
||||
} |
||||
|
||||
fail: |
||||
av_frame_free(&frame); |
||||
avfilter_graph_free(&in_graph); |
||||
if (streams) { |
||||
for (i = 0; i < nb_out_dev; i++) { |
||||
st = &streams[i]; |
||||
if (st->mux) { |
||||
if (st->mux->pb) |
||||
avio_close(st->mux->pb); |
||||
avformat_free_context(st->mux); |
||||
} |
||||
} |
||||
} |
||||
av_freep(&streams); |
||||
return ret < 0; |
||||
} |
Loading…
Reference in new issue