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 "libavutil/log.h"
25 #include "libavutil/intreadwrite.h"
26 
27 typedef struct SCCContext {
29  int inside;
30  int n;
31 } SCCContext;
32 
34 {
35  SCCContext *scc = avf->priv_data;
36 
37  if (avf->nb_streams != 1 ||
39  av_log(avf, AV_LOG_ERROR,
40  "SCC supports only a single subtitles stream.\n");
41  return AVERROR(EINVAL);
42  }
43  if (avf->streams[0]->codecpar->codec_id != AV_CODEC_ID_EIA_608) {
44  av_log(avf, AV_LOG_ERROR,
45  "Unsupported subtitles codec: %s\n",
47  return AVERROR(EINVAL);
48  }
49  avpriv_set_pts_info(avf->streams[0], 64, 1, 1000);
50  avio_printf(avf->pb, "Scenarist_SCC V1.0\n");
51 
52  scc->prev_h = scc->prev_m = scc->prev_s = scc->prev_f = -1;
53  scc->inside = 0;
54 
55  return 0;
56 }
57 
59 {
60  SCCContext *scc = avf->priv_data;
61  int64_t pts = pkt->pts;
62  int i, h, m, s, f;
63 
64  if (pts == AV_NOPTS_VALUE) {
66  "Insufficient timestamps.\n");
67  return 0;
68  }
69 
70  h = (int)(pts / (3600000));
71  m = (int)(pts / (60000)) % 60;
72  s = (int)(pts / 1000) % 60;
73  f = (int)(pts % 1000) / 33;
74 
75  for (i = 0; i < pkt->size; i+=3) {
76  if (pkt->data[i] == 0xfc && ((pkt->data[i + 1] != 0x80 || pkt->data[i + 2] != 0x80)))
77  break;
78  }
79  if (i >= pkt->size)
80  return 0;
81 
82  if (!scc->inside && (scc->prev_h != h || scc->prev_m != m || scc->prev_s != s || scc->prev_f != f)) {
83  avio_printf(avf->pb, "\n%02d:%02d:%02d:%02d\t", h, m, s, f);
84  scc->inside = 1;
85  }
86  for (i = 0; i < pkt->size; i+=3) {
87  if (i + 3 > pkt->size)
88  break;
89 
90  if (pkt->data[i] != 0xfc || (pkt->data[i + 1] == 0x80 && pkt->data[i + 2] == 0x80))
91  continue;
92  if (!scc->inside) {
93  avio_printf(avf->pb, "\n%02d:%02d:%02d:%02d\t", h, m, s, f);
94  scc->inside = 1;
95  }
96  if (scc->n > 0)
97  avio_printf(avf->pb, " ");
98  avio_printf(avf->pb, "%02x%02x", pkt->data[i + 1], pkt->data[i + 2]);
99  scc->n++;
100  }
101  if (scc->inside && (scc->prev_h != h || scc->prev_m != m || scc->prev_s != s || scc->prev_f != f)) {
102  avio_printf(avf->pb, "\n");
103  scc->n = 0;
104  scc->inside = 0;
105  }
106 
107  scc->prev_h = h;
108  scc->prev_m = m;
109  scc->prev_s = s;
110  scc->prev_f = f;
111  return 0;
112 }
113 
115  .name = "scc",
116  .long_name = NULL_IF_CONFIG_SMALL("Scenarist Closed Captions"),
117  .extensions = "scc",
118  .priv_data_size = sizeof(SCCContext),
122  .subtitle_codec = AV_CODEC_ID_EIA_608,
123 };
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:689
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4896
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3960
int size
Definition: avcodec.h:1481
int prev_f
Definition: sccenc.c:28
static AVPacket pkt
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:477
Format I/O context.
Definition: avformat.h:1358
static int scc_write_header(AVFormatContext *avf)
Definition: sccenc.c:33
#define f(width, name)
Definition: cbs_vp9.c:255
int prev_m
Definition: sccenc.c:28
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1426
uint8_t * data
Definition: avcodec.h:1480
int inside
Definition: sccenc.c:29
AVOutputFormat ff_scc_muxer
Definition: sccenc.c:114
#define av_log(a,...)
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3956
static int scc_write_packet(AVFormatContext *avf, AVPacket *pkt)
Definition: sccenc.c:58
int prev_h
Definition: sccenc.c:28
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1414
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:466
const char * name
Definition: avformat.h:505
#define s(width, name)
Definition: cbs_vp9.c:257
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:1150
int n
Definition: sccenc.c:30
AVIOContext * pb
I/O context.
Definition: avformat.h:1400
static int64_t pts
#define flags(name, subs,...)
Definition: cbs_av1.c:561
int prev_s
Definition: sccenc.c:28
Main libavformat public API header.
int
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:470
void * priv_data
Format private data.
Definition: avformat.h:1386
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:337
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1028
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
This structure stores compressed data.
Definition: avcodec.h:1457
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1473
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
Writes a formatted string to the context.