FFmpeg
supenc.c
Go to the documentation of this file.
1 /*
2  * SUP muxer
3  * Copyright (c) 2014 Petri Hintukainen <phintuka@users.sourceforge.net>
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/intreadwrite.h"
25 
26 #define SUP_PGS_MAGIC 0x5047 /* "PG", big endian */
27 
29 {
30  uint8_t *data = pkt->data;
31  size_t size = pkt->size;
32  uint32_t pts = 0, dts = 0;
33 
34  if (pkt->pts != AV_NOPTS_VALUE) {
35  pts = pkt->pts;
36  }
37  if (pkt->dts != AV_NOPTS_VALUE) {
38  dts = pkt->dts;
39  }
40 
41  /*
42  Split frame to segments.
43  mkvmerge stores multiple segments in one frame.
44  */
45  while (size > 2) {
46  size_t len = AV_RB16(data + 1) + 3;
47 
48  if (len > size) {
49  av_log(s, AV_LOG_ERROR, "Not enough data, skipping %"SIZE_SPECIFIER" bytes\n",
50  size);
51  return AVERROR_INVALIDDATA;
52  }
53 
54  /* header */
56  avio_wb32(s->pb, pts);
57  avio_wb32(s->pb, dts);
58 
59  avio_write(s->pb, data, len);
60 
61  data += len;
62  size -= len;
63  }
64 
65  if (size > 0) {
66  av_log(s, AV_LOG_ERROR, "Skipping %"SIZE_SPECIFIER" bytes after last segment in frame\n",
67  size);
68  return AVERROR_INVALIDDATA;
69  }
70 
71  return 0;
72 }
73 
75 {
76  if (s->nb_streams != 1) {
77  av_log(s, AV_LOG_ERROR, "%s files have exactly one stream\n",
78  s->oformat->name);
79  return AVERROR(EINVAL);
80  }
81 
82  avpriv_set_pts_info(s->streams[0], 32, 1, 90000);
83 
84  return 0;
85 }
86 
88  .name = "sup",
89  .long_name = NULL_IF_CONFIG_SMALL("raw HDMV Presentation Graphic Stream subtitles"),
90  .extensions = "sup",
91  .mime_type = "application/x-pgs",
92  .subtitle_codec = AV_CODEC_ID_HDMV_PGS_SUBTITLE,
93  .write_header = sup_write_header,
94  .write_packet = sup_write_packet,
96 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static int sup_write_header(AVFormatContext *s)
Definition: supenc.c:74
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
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:4929
int size
Definition: avcodec.h:1481
static AVPacket pkt
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
#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
#define SUP_PGS_MAGIC
Definition: supenc.c:26
uint8_t
static int sup_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: supenc.c:28
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1426
uint8_t * data
Definition: avcodec.h:1480
ptrdiff_t size
Definition: opengl_enc.c:100
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:218
#define av_log(a,...)
#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
AVOutputFormat ff_sup_muxer
Definition: supenc.c:87
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1414
const char * name
Definition: avformat.h:505
#define s(width, name)
Definition: cbs_vp9.c:257
ff_const59 struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1377
AVIOContext * pb
I/O context.
Definition: avformat.h:1400
static int64_t pts
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:478
#define SIZE_SPECIFIER
Definition: internal.h:262
Main libavformat public API header.
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:470
int len
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1479
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:380
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