timecode: introduce timecode and honor it in MPEG-1/2.

This is based on the original work by Baptiste Coudurier.
This commit is contained in:
Clément Bœsch 2011-07-06 14:44:59 +02:00 committed by Clément Bœsch
parent 78da04384a
commit 5231454560
6 changed files with 150 additions and 8 deletions

View File

@ -260,6 +260,7 @@ OBJS-$(CONFIG_MPEG_XVMC_DECODER) += mpegvideo_xvmc.o
OBJS-$(CONFIG_MPEG1VIDEO_DECODER) += mpeg12.o mpeg12data.o \
mpegvideo.o error_resilience.o
OBJS-$(CONFIG_MPEG1VIDEO_ENCODER) += mpeg12enc.o mpegvideo_enc.o \
timecode.o \
motion_est.o ratecontrol.o \
mpeg12.o mpeg12data.o \
mpegvideo.o error_resilience.o
@ -268,6 +269,7 @@ OBJS-$(CONFIG_MPEG2_VAAPI_HWACCEL) += vaapi_mpeg2.o
OBJS-$(CONFIG_MPEG2VIDEO_DECODER) += mpeg12.o mpeg12data.o \
mpegvideo.o error_resilience.o
OBJS-$(CONFIG_MPEG2VIDEO_ENCODER) += mpeg12enc.o mpegvideo_enc.o \
timecode.o \
motion_est.o ratecontrol.o \
mpeg12.o mpeg12data.o \
mpegvideo.o error_resilience.o

View File

@ -29,11 +29,11 @@
#endif
/* MpegEncContext */
#define Y_DC_SCALE 0xa8
#define C_DC_SCALE 0xac
#define AC_PRED 0xb0
#define BLOCK_LAST_INDEX 0xb4
#define H263_AIC 0xe4
#define INTER_SCANTAB_RASTER_END 0x12c
#define Y_DC_SCALE 0xac
#define C_DC_SCALE 0xb0
#define AC_PRED 0xb4
#define BLOCK_LAST_INDEX 0xb8
#define H263_AIC 0xe8
#define INTER_SCANTAB_RASTER_END 0x130
#endif /* AVCODEC_ARM_ASM_OFFSETS_H */

View File

@ -32,6 +32,8 @@
#include "mpeg12.h"
#include "mpeg12data.h"
#include "bytestream.h"
#include "timecode.h"
#include "libavutil/opt.h"
static const uint8_t inv_non_linear_qscale[13] = {
@ -172,11 +174,18 @@ static av_cold int encode_init(AVCodecContext *avctx)
}
}
s->tc.drop = !!(avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE);
if((avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE) && s->frame_rate_index != 4){
av_log(avctx, AV_LOG_ERROR, "Drop frame time code only allowed with 1001/30000 fps\n");
return -1;
}
if (s->tc.str) {
s->tc.rate = ff_frame_rate_tab[s->frame_rate_index];
if (ff_init_smtpe_timecode(s, &s->tc) < 0)
return -1;
s->avctx->timecode_frame_start = s->tc.start;
}
return 0;
}
@ -283,14 +292,14 @@ static void mpeg1_encode_sequence_header(MpegEncContext *s)
}
put_header(s, GOP_START_CODE);
put_bits(&s->pb, 1, !!(s->avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE)); /* drop frame flag */
put_bits(&s->pb, 1, s->tc.drop);
/* time code : we must convert from the real frame rate to a
fake mpeg frame rate in case of low frame rate */
fps = (framerate.num + framerate.den/2)/ framerate.den;
time_code = s->current_picture_ptr->f.coded_picture_number + s->avctx->timecode_frame_start;
s->gop_picture_number = s->current_picture_ptr->f.coded_picture_number;
if (s->avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE) {
if (s->tc.drop) {
/* only works for NTSC 29.97 */
int d = time_code / 17982;
int m = time_code % 17982;
@ -925,6 +934,17 @@ static void mpeg1_encode_block(MpegEncContext *s,
put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
}
static const AVClass class = {
.class_name = "mpegvideo",
.item_name = av_default_item_name,
.version = LIBAVUTIL_VERSION_INT,
.option = (const AVOption[]){
{TIMECODE_OPT(MpegEncContext,
AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)},
{NULL}
},
};
AVCodec ff_mpeg1video_encoder = {
.name = "mpeg1video",
.type = AVMEDIA_TYPE_VIDEO,
@ -937,6 +957,7 @@ AVCodec ff_mpeg1video_encoder = {
.pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
.capabilities= CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
.long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
.priv_class = &class,
};
AVCodec ff_mpeg2video_encoder = {
@ -951,4 +972,5 @@ AVCodec ff_mpeg2video_encoder = {
.pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_NONE},
.capabilities= CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
.long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
.priv_class = &class,
};

View File

@ -36,6 +36,7 @@
#include "parser.h"
#include "mpeg12data.h"
#include "rl.h"
#include "timecode.h"
#define FRAME_SKIPPED 100 ///< return value for header parsers if frame is not coded
@ -199,6 +200,7 @@ typedef struct MotionEstContext{
* MpegEncContext.
*/
typedef struct MpegEncContext {
AVClass *av_class;
struct AVCodecContext *avctx;
/* the following parameters must be initialized before encoding */
int width, height;///< picture size. must be a multiple of 16
@ -647,6 +649,8 @@ typedef struct MpegEncContext {
/* RTP specific */
int rtp_mode;
struct ff_timecode tc;
uint8_t *ptr_lastgob;
int swap_uv; //vcr2 codec is an MPEG-2 variant with U and V swapped
DCTELEM (*pblocks[12])[64];

56
libavcodec/timecode.c Normal file
View File

@ -0,0 +1,56 @@
/*
* Copyright (C) 2006 Smartjog S.A.S, Baptiste Coudurier <baptiste.coudurier@gmail.com>
* Copyright (C) 2011 Smartjog S.A.S, Clément Bœsch <clement.boesch@smartjog.com>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation;
* version 2 of the License.
*
* 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
* General Public License for more details.
*
* You should have received a copy of the GNU 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
*/
/**
* @file
* Timecode helpers
*/
#include <stdio.h>
#include "timecode.h"
#include "libavutil/log.h"
int ff_init_smtpe_timecode(void *avcl, struct ff_timecode *tc)
{
int hh, mm, ss, ff, fps;
char c;
if (sscanf(tc->str, "%d:%d:%d%c%d", &hh, &mm, &ss, &c, &ff) != 5) {
av_log(avcl, AV_LOG_ERROR, "unable to parse timecode, "
"syntax: hh:mm:ss[:;.]ff\n");
return -1;
}
fps = (tc->rate.num + tc->rate.den/2) / tc->rate.den;
tc->start = (hh*3600 + mm*60 + ss) * fps + ff;
tc->drop = c != ':'; // drop if ';', '.', ...
if (tc->drop) { /* adjust frame number */
int tmins = 60*hh + mm;
if (tc->rate.den != 1001 || fps != 30) {
av_log(avcl, AV_LOG_ERROR, "error: drop frame is only allowed with"
"30000/1001 FPS");
return -2;
}
tc->start -= 2 * (tmins - tmins/10);
}
return 0;
}

58
libavcodec/timecode.h Normal file
View File

@ -0,0 +1,58 @@
/*
* Copyright (C) 2006 Smartjog S.A.S, Baptiste Coudurier <baptiste.coudurier@gmail.com>
* Copyright (C) 2011 Smartjog S.A.S, Clément Bœsch <clement.boesch@smartjog.com>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation;
* version 2 of the License.
*
* 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
* General Public License for more details.
*
* You should have received a copy of the GNU 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
*/
/**
* @file
* Timecode helpers header
*/
#ifndef AVCODEC_TIMECODE_H
#define AVCODEC_TIMECODE_H
#include <stdint.h>
#include "libavutil/rational.h"
#define TIMECODE_OPT(ctx, flags) \
"timecode", "set timecode value following hh:mm:ss[:;.]ff format, " \
"use ';' or '.' before frame number for drop frame", \
offsetof(ctx, tc.str), \
FF_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, flags
struct ff_timecode {
char *str; ///< string following the hh:mm:ss[:;.]ff format
int start; ///< timecode frame start
int drop; ///< drop flag (1 if drop, else 0)
AVRational rate; ///< Frame rate in rationnal form
};
/**
* Parse SMTPE 12M time representation (hh:mm:ss[:;.]ff). str and rate fields
* from tc struct must be set.
*
* @param avcl A pointer to an arbitrary struct of which the first field is a
* pointer to an AVClass struct (used for av_log).
* @param tc Timecode struct pointer
* @return 0 on success, negative value on failure
* @warning Adjustement is only valid in NTSC 29.97
*/
int ff_init_smtpe_timecode(void *avcl, struct ff_timecode *tc);
#endif /* AVCODEC_TIMECODE_H */