/*
* FLV demuxer
* Copyright (c) 2003 The FFmpeg Project
*
* This demuxer will generate a 1 byte extradata for VP6F content.
* It is composed of:
* - upper 4 bits: difference between encoded width and visible width
* - lower 4 bits: difference between encoded height and visible height
*
* 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/avstring.h"
#include "libavutil/channel_layout.h"
#include "libavutil/dict.h"
#include "libavutil/opt.h"
#include "libavutil/internal.h"
#include "libavutil/intfloat.h"
#include "libavutil/mathematics.h"
#include "libavutil/time_internal.h"
#include "libavcodec/bytestream.h"
#include "avformat.h"
#include "internal.h"
#include "avio_internal.h"
#include "flv.h"
#define VALIDATE_INDEX_TS_THRESH 2500
#define RESYNC_BUFFER_SIZE (1<<20)
#define MAX_DEPTH 16 ///< arbitrary limit to prevent unbounded recursion
typedef struct FLVContext {
const AVClass *class; ///< Class for private options.
int trust_metadata; ///< configure streams according onMetaData
int trust_datasize; ///< trust data size of FLVTag
int dump_full_metadata; ///< Dump full metadata of the onMetadata
int wrong_dts; ///< wrong dts due to negative cts
uint8_t *new_extradata[FLV_STREAM_TYPE_NB];
int new_extradata_size[FLV_STREAM_TYPE_NB];
int last_sample_rate;
int last_channels;
struct {
int64_t dts;
int64_t pos;
} validate_index[2];
int validate_next;
int validate_count;
int searched_for_end;
uint8_t resync_buffer[2*RESYNC_BUFFER_SIZE];
int broken_sizes;
int sum_flv_tag_size;
int last_keyframe_stream_index;
int keyframe_count;
int64_t video_bit_rate;
int64_t audio_bit_rate;
int64_t *keyframe_times;
int64_t *keyframe_filepositions;
int missing_streams;
AVRational framerate;
int64_t last_ts;
int64_t time_offset;
int64_t time_pos;
} FLVContext;
/* AMF date type */
typedef struct amf_date {
double milliseconds;
int16_t timezone;
} amf_date;
static int probe(const AVProbeData *p, int live)
{
const uint8_t *d = p->buf;
unsigned offset = AV_RB32(d + 5);
if (d[0] == 'F' &&
d[1] == 'L' &&
d[2] == 'V' &&
d[3] < 5 && d[5] == 0 &&
offset + 100 < p->buf_size &&
offset > 8) {
int is_live = !memcmp(d + offset + 40, "NGINX RTMP", 10);
if (live == is_live)
return AVPROBE_SCORE_MAX;
}
return 0;
}
static int flv_probe(const AVProbeData *p)
{
return probe(p, 0);
}
static int live_flv_probe(const AVProbeData *p)
{
return probe(p, 1);
}
static int kux_probe(const AVProbeData *p)
{
const uint8_t *d = p->buf;
if (d[0] == 'K' &&
d[1] == 'D' &&
d[2] == 'K' &&
d[3] == 0 &&
d[4] == 0) {
return AVPROBE_SCORE_EXTENSION + 1;
}
return 0;
}
static void add_keyframes_index(AVFormatContext *s)
{
FLVContext *flv = s->priv_data;
AVStream *stream = NULL;
unsigned int i = 0;
if (flv->last_keyframe_stream_index < 0) {
av_log(s, AV_LOG_DEBUG, "keyframe stream hasn't been created\n");
return;
}
av_assert0(flv->last_keyframe_stream_index <= s->nb_streams);
stream = s->streams[flv->last_keyframe_stream_index];
if (ffstream(stream)->nb_index_entries == 0) {
for (i = 0; i < flv->keyframe_count; i++) {
av_log(s, AV_LOG_TRACE, "keyframe filepositions = %"PRId64" times = %"PRId64"\n",
flv->keyframe_filepositions[i], flv->keyframe_times[i] * 1000);
av_add_index_entry(stream, flv->keyframe_filepositions[i],
flv->keyframe_times[i] * 1000, 0, 0, AVINDEX_KEYFRAME);
}
} else
av_log(s, AV_LOG_WARNING, "Skipping duplicate index\n");
if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
av_freep(&flv->keyframe_times);
av_freep(&flv->keyframe_filepositions);
flv->keyframe_count = 0;
}
}
static AVStream *create_stream(AVFormatContext *s, int codec_type)
{
FLVContext *flv = s->priv_data;
AVStream *st = avformat_new_stream(s, NULL);
if (!st)
return NULL;
st->codecpar->codec_type = codec_type;
if (s->nb_streams>=3 ||( s->nb_streams==2
&& s->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE
&& s->streams[1]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE
&& s->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_DATA
&& s->streams[1]->codecpar->codec_type != AVMEDIA_TYPE_DATA))
s->ctx_flags &= ~AVFMTCTX_NOHEADER;
if (codec_type == AVMEDIA_TYPE_AUDIO) {
st->codecpar->bit_rate = flv->audio_bit_rate;
flv->missing_streams &= ~FLV_HEADER_FLAG_HASAUDIO;
}
if (codec_type == AVMEDIA_TYPE_VIDEO) {
st->codecpar->bit_rate = flv->video_bit_rate;
flv->missing_streams &= ~FLV_HEADER_FLAG_HASVIDEO;
st->avg_frame_rate = flv->framerate;
}
avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
flv->last_keyframe_stream_index = s->nb_streams - 1;
add_keyframes_index(s);
return st;
}
static int flv_same_audio_codec(AVCodecParameters *apar, int flags)
{
int bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
int flv_codecid = flags & FLV_AUDIO_CODECID_MASK;
int codec_id;
if (!apar->codec_id && !apar->codec_tag)
return 1;
if (apar->bits_per_coded_sample != bits_per_coded_sample)
return 0;
switch (flv_codecid) {
// no distinction between S16 and S8 PCM codec flags
case FLV_CODECID_PCM:
codec_id = bits_per_coded_sample == 8
? AV_CODEC_ID_PCM_U8
#if HAVE_BIGENDIAN
: AV_CODEC_ID_PCM_S16BE;
#else
: AV_CODEC_ID_PCM_S16LE;
#endif
return codec_id == apar->codec_id;
case FLV_CODECID_PCM_LE:
codec_id = bits_per_coded_sample == 8
? AV_CODEC_ID_PCM_U8
: AV_CODEC_ID_PCM_S16LE;
return codec_id == apar->codec_id;
case FLV_CODECID_AAC:
return apar->codec_id == AV_CODEC_ID_AAC;
case FLV_CODECID_ADPCM:
return apar->codec_id == AV_CODEC_ID_ADPCM_SWF;
case FLV_CODECID_SPEEX:
return apar->codec_id == AV_CODEC_ID_SPEEX;
case FLV_CODECID_MP3:
return apar->codec_id == AV_CODEC_ID_MP3;
case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
case FLV_CODECID_NELLYMOSER:
return apar->codec_id == AV_CODEC_ID_NELLYMOSER;
case FLV_CODECID_PCM_MULAW:
return apar->sample_rate == 8000 &&
apar->codec_id == AV_CODEC_ID_PCM_MULAW;
case FLV_CODECID_PCM_ALAW:
return apar->sample_rate == 8000 &&
apar->codec_id == AV_CODEC_ID_PCM_ALAW;
default:
return apar->codec_tag == (flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
}
}
static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream,
AVCodecParameters *apar, int flv_codecid)
{
switch (flv_codecid) {
// no distinction between S16 and S8 PCM codec flags
case FLV_CODE
ffmpeg 编译 支持 h265 rtmp 推流
5星 · 超过95%的资源 需积分: 41 159 浏览量
2022-08-04
11:35:39
上传
评论 2
收藏 89.2MB ZIP 举报
蜗牛的信仰
- 粉丝: 22
- 资源: 27
最新资源
- F103-霸道开发板2.8寸电阻触摸屏例程.rar
- Google(高德)地图瓦片python代码下载
- Python实现输出杨辉三角形
- polsarpro官方教程、操作说明 PolSARpro v5.0 Software Training Course
- STM32 TouchGFX的使用二图片显示
- buildx镜像文件,也可以通过网上其他方式获取
- 【中级软件设计师】上午题12-软件工程(2):单元测试、黑盒测试、白盒测试、软件运行与维护
- 免费计算机毕业设计-医院住院管理系统的设计与实现(包含代码+论文)
- tt100k数据转换yolo格式
- 免费计算机毕业设计-学生在线网络考试系统的设计与实现(包含论文+源码)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论5