When you record or stream video encoded as MPEG-2 or H.264, the writer can put the captions of the frames into the video stream as ATSC A/53 caption data. Players, set-top boxes and caption tools read them from there. This article shows how to enable it and how to replace or remove the captions of a source on the way.
Enabling caption embedding
Set embed_cc to true on MFWriter or MWriter, either as a property or in the configuration string:
// MFormats
writer.PropsSet("embed_cc", "true");
writer.WriterSet(@"C:\media\out.ts", 0, "format='mpegts' video::codec='mpeg2video' audio::codec='mp2'");
// or in the configuration string
writer.WriterSet(@"C:\media\out.mp4", 0, "format='mp4' embed_cc='true' video::codec='libx264' audio::codec='aac'");
// MPlatform
m_objWriter.WriterNameSet("udp://239.0.0.1:1234", "format='mpegts' embed_cc='true' video::codec='mpeg2video'");
Rules:
- Captions are embedded into MPEG-2 and H.264 video, in any container and network protocol that carries them (TS, MP4, MOV, MXF, UDP, SRT and others). Other codecs, such as HEVC, are not supported.
- The captions of each frame are taken from
GA94,C708orC608, in this order, and converted to A/53 data. A CDP keeps its CEA-708 services; CEA-608-only data is wrapped as CEA-608 inside A/53. - In MPEG-2 video the captions are placed after the picture coding extension and before the first slice, as ATSC A/53 requires. A video packet without such a place is written without captions, and a warning with its PTS and DTS is logged.
- Embedding also works when the video is not re-encoded (
video::codec='packets'): the writer matches each video packet with the captions of its frame.
The writer cleans up the caption data before embedding (refine_cc, default true): it converts a CDP to
A/53, fixes the length of A/53 data and turns a bare 4-byte CEA-608 value into A/53. Leave it on unless your data is
already final A/53 user data.
Replacing and adding captions
To give a video your own captions, set the caption data on each frame before it goes to the writer
(or to an SDI renderer). Because the writer uses GA94 and C708 before C608,
first remove all caption keys of the source, then set yours.
A small helper to copy a byte array into frame data:
using System;
using System.Runtime.InteropServices;
using MFORMATSLib;
static class FrameData
{
public static void Set(IMFFrame frame, string key, byte[] data)
{
IntPtr buffer = Marshal.AllocHGlobal(data.Length);
try
{
Marshal.Copy(data, 0, buffer, data.Length);
frame.MFDataSet(key, data.Length, buffer.ToInt64()); // the frame keeps its own copy
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
public static void RemoveCaptions(IMFFrame frame)
{
foreach (string key in new[] { "C608", "C708", "GA94", "ATSC" })
frame.MFDataSet(key, -1, 0);
}
}
MFormats
reader.SourceFrameGet(-1, out MFFrame frame, "");
FrameData.RemoveCaptions(frame);
// CEA-608 for this frame: field 1 = 0x94 0x2C (erase displayed captions on CC1), field 2 = empty
FrameData.Set(frame, "C608", new byte[] { 0x94, 0x2C, 0x80, 0x80 });
writer.ReceiverFramePut(frame, -1, "");
Marshal.ReleaseComObject(frame);
MPlatform
m_objFile.PropsSet("object::on_frame.sync", "true");
m_objFile.PropsSet("object::on_frame.data", "true");
m_objFile.OnFrameSafe += (string channelId, object pFrame) =>
{
IMFFrame frame = (IMFFrame)pFrame;
FrameData.RemoveCaptions(frame);
FrameData.Set(frame, "C608", NextCaptionBytes()); // your caption source
Marshal.ReleaseComObject(pFrame);
};
CEA-608 sends two bytes per field per frame, so the text of a caption is spread over many frames. How to build
these bytes from text is shown in Generating CEA-608 captions in code. If you already have
ready-made CEA-708 data, set C708 (a CDP with the ccdp tag) or GA94 instead.
Removing captions
To deliver a video without captions, remove the caption keys from every frame, as in
FrameData.RemoveCaptions above. With embed_cc off, the writer does not embed captions anyway,
but it still writes an SCC sidecar file when captions are present (see Closed captions in SCC, MCC and ANC sidecar files).
Removing the keys does not remove captions that are already inside a video stream that is copied without re-encoding
(video::codec='packets'). Re-encode the video to drop them.
XDS: program name and rating
With caption embedding, the writer can also insert XDS data (program name and V-Chip rating). See XDS: program name and V-Chip rating.
Still have questions?
Tell us what didn’t work or what you’d like clarified.