FFmpeg
sccenc.c
Go to the documentation of this file.
1 /*
2  * SCC muxer
3  * Copyright (c) 2017 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "avformat.h"
23 #include "internal.h"
24 #include "mux.h"
25 #include "libavutil/log.h"
26 #include "libavutil/intreadwrite.h"
27 
28 typedef struct SCCContext {
30  int inside;
31  int n;
32 } SCCContext;
33 
35 {
36  SCCContext *scc = avf->priv_data;
37 
38  if (avf->nb_streams != 1 ||
40  av_log(avf, AV_LOG_ERROR,
41  "SCC supports only a single subtitles stream.\n");
42  return AVERROR(EINVAL);
43  }
44  if (avf->streams[0]->codecpar->codec_id != AV_CODEC_ID_EIA_608) {
45  av_log(avf, AV_LOG_ERROR,
46  "Unsupported subtitles codec: %s\n",
48  return AVERROR(EINVAL);
49  }
50  avpriv_set_pts_info(avf->streams[0], 64, 1, 1000);
51  avio_printf(avf->pb, "Scenarist_SCC V1.0\n");
52 
53  scc->prev_h = scc->prev_m = scc->prev_s = scc->prev_f = -1;
54  scc->inside = 0;
55 
56  return 0;
57 }
58 
60 {
61  SCCContext *scc = avf->priv_data;
62  int64_t pts = pkt->pts;
63  int i, h, m, s, f;
64 
65  if (pts == AV_NOPTS_VALUE) {
67  "Insufficient timestamps.\n");
68  return 0;
69  }
70 
71  h = (int)(pts / (3600000));
72  m = (int)(pts / (60000)) % 60;
73  s = (int)(pts / 1000) % 60;
74  f = (int)(pts % 1000) / 33;
75 
76  for (i = 0; i < pkt->size - 2; i+=3) {
77  if (pkt->data[i] == 0xfc && ((pkt->data[i + 1] != 0x80 || pkt->data[i + 2] != 0x80)))
78  break;
79  }
80  if (i >= pkt->size - 2)
81  return 0;
82 
83  if (!scc->inside && (scc->prev_h != h || scc->prev_m != m || scc->prev_s != s || scc->prev_f != f)) {
84  avio_printf(avf->pb, "\n%02d:%02d:%02d:%02d\t", h, m, s, f);
85  scc->inside = 1;
86  }
87  for (i = 0; i < pkt->size; i+=3) {
88  if (i + 3 > pkt->size)
89  break;
90 
91  if (pkt->data[i] != 0xfc || (pkt->data[i + 1] == 0x80 && pkt->data[i + 2] == 0x80))
92  continue;
93  if (!scc->inside) {
94  avio_printf(avf->pb, "\n%02d:%02d:%02d:%02d\t", h, m, s, f);
95  scc->inside = 1;
96  }
97  if (scc->n > 0)
98  avio_w8(avf->pb, ' ');
99  avio_printf(avf->pb, "%02x%02x", pkt->data[i + 1], pkt->data[i + 2]);
100  scc->n++;
101  }
102  if (scc->inside && (scc->prev_h != h || scc->prev_m != m || scc->prev_s != s || scc->prev_f != f)) {
103  avio_w8(avf->pb, '\n');
104  scc->n = 0;
105  scc->inside = 0;
106  }
107 
108  scc->prev_h = h;
109  scc->prev_m = m;
110  scc->prev_s = s;
111  scc->prev_f = f;
112  return 0;
113 }
114 
116  .p.name = "scc",
117  .p.long_name = NULL_IF_CONFIG_SMALL("Scenarist Closed Captions"),
118  .p.extensions = "scc",
120  .p.subtitle_codec = AV_CODEC_ID_EIA_608,
121  .priv_data_size = sizeof(SCCContext),
124 };
AV_CODEC_ID_EIA_608
@ AV_CODEC_ID_EIA_608
Definition: codec_id.h:560
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVOutputFormat::name
const char * name
Definition: avformat.h:511
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
scc_write_packet
static int scc_write_packet(AVFormatContext *avf, AVPacket *pkt)
Definition: sccenc.c:59
AVFMT_VARIABLE_FPS
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:483
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1183
AVPacket::data
uint8_t * data
Definition: packet.h:491
SCCContext::prev_h
int prev_h
Definition: sccenc.c:29
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:36
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:761
pts
static int64_t pts
Definition: transcode_aac.c:643
SCCContext
Definition: sccdec.c:29
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
SCCContext::prev_m
int prev_m
Definition: sccenc.c:29
AVFormatContext
Format I/O context.
Definition: avformat.h:1115
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:864
ff_scc_muxer
const FFOutputFormat ff_scc_muxer
Definition: sccenc.c:115
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1157
FFOutputFormat
Definition: mux.h:32
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:226
SCCContext::n
int n
Definition: sccenc.c:31
AVFormatContext::nb_streams
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1171
f
f
Definition: af_crystalizer.c:121
AVPacket::size
int size
Definition: packet.h:492
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
avcodec_get_name
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:417
log.h
AVFMT_GLOBALHEADER
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:479
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:484
write_packet
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
Definition: ffmpeg_mux.c:61
AVFMT_TS_NONSTRICT
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:492
SCCContext::prev_s
int prev_s
Definition: sccenc.c:29
avformat.h
avio_printf
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
Writes a formatted string to the context.
scc_write_header
static int scc_write_header(AVFormatContext *avf)
Definition: sccenc.c:34
SCCContext::inside
int inside
Definition: sccenc.c:30
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:468
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
h
h
Definition: vp9dsp_template.c:2038
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:346
int
int
Definition: ffmpeg_filter.c:368
SCCContext::prev_f
int prev_f
Definition: sccenc.c:29
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1143
mux.h