vf_readeia608.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (c) 2017 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Filter for reading closed captioning data (EIA-608).
  23. * See also https://en.wikipedia.org/wiki/EIA-608
  24. */
  25. #include <string.h>
  26. #include "libavutil/internal.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "libavutil/timestamp.h"
  30. #include "avfilter.h"
  31. #include "formats.h"
  32. #include "internal.h"
  33. #include "video.h"
  34. #define FALL 0
  35. #define RISE 1
  36. typedef struct ReadEIA608Context {
  37. const AVClass *class;
  38. int start, end;
  39. int min_range;
  40. int max_peak_diff;
  41. int max_period_diff;
  42. int max_start_diff;
  43. int nb_found;
  44. int white;
  45. int black;
  46. float mpd, mhd, msd, mac, spw, bhd, wth, bth;
  47. int chp;
  48. int lp;
  49. uint8_t *temp;
  50. } ReadEIA608Context;
  51. #define OFFSET(x) offsetof(ReadEIA608Context, x)
  52. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  53. static const AVOption readeia608_options[] = {
  54. { "scan_min", "set from which line to scan for codes", OFFSET(start), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
  55. { "scan_max", "set to which line to scan for codes", OFFSET(end), AV_OPT_TYPE_INT, {.i64=29}, 0, INT_MAX, FLAGS },
  56. { "mac", "set minimal acceptable amplitude change for sync codes detection", OFFSET(mac), AV_OPT_TYPE_FLOAT, {.dbl=.2}, 0.001, 1, FLAGS },
  57. { "spw", "set ratio of width reserved for sync code detection", OFFSET(spw), AV_OPT_TYPE_FLOAT, {.dbl=.27}, 0.1, 0.7, FLAGS },
  58. { "mhd", "set max peaks height difference for sync code detection", OFFSET(mhd), AV_OPT_TYPE_FLOAT, {.dbl=.1}, 0, 0.5, FLAGS },
  59. { "mpd", "set max peaks period difference for sync code detection", OFFSET(mpd), AV_OPT_TYPE_FLOAT, {.dbl=.1}, 0, 0.5, FLAGS },
  60. { "msd", "set first two max start code bits differences", OFFSET(msd), AV_OPT_TYPE_FLOAT, {.dbl=.02}, 0, 0.5, FLAGS },
  61. { "bhd", "set min ratio of bits height compared to 3rd start code bit", OFFSET(bhd), AV_OPT_TYPE_FLOAT, {.dbl=.75}, 0.01, 1, FLAGS },
  62. { "th_w", "set white color threshold", OFFSET(wth), AV_OPT_TYPE_FLOAT, {.dbl=.35}, 0.1, 1, FLAGS },
  63. { "th_b", "set black color threshold", OFFSET(bth), AV_OPT_TYPE_FLOAT, {.dbl=.15}, 0, 0.5, FLAGS },
  64. { "chp", "check and apply parity bit", OFFSET(chp), AV_OPT_TYPE_BOOL, {.i64= 0}, 0, 1, FLAGS },
  65. { "lp", "lowpass line prior to processing", OFFSET(lp), AV_OPT_TYPE_BOOL, {.i64= 0}, 0, 1, FLAGS },
  66. { NULL }
  67. };
  68. AVFILTER_DEFINE_CLASS(readeia608);
  69. static int query_formats(AVFilterContext *ctx)
  70. {
  71. static const enum AVPixelFormat pixel_fmts[] = {
  72. AV_PIX_FMT_GRAY8,
  73. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  74. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  75. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  76. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  77. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  78. AV_PIX_FMT_YUVJ411P,
  79. AV_PIX_FMT_NONE
  80. };
  81. AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
  82. if (!formats)
  83. return AVERROR(ENOMEM);
  84. return ff_set_common_formats(ctx, formats);
  85. }
  86. static int config_input(AVFilterLink *inlink)
  87. {
  88. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  89. AVFilterContext *ctx = inlink->dst;
  90. ReadEIA608Context *s = ctx->priv;
  91. int depth = desc->comp[0].depth;
  92. if (s->end >= inlink->h) {
  93. av_log(ctx, AV_LOG_WARNING, "Last line to scan too large, clipping.\n");
  94. s->end = inlink->h - 1;
  95. }
  96. if (s->start > s->end) {
  97. av_log(ctx, AV_LOG_ERROR, "Invalid range.\n");
  98. return AVERROR(EINVAL);
  99. }
  100. s->min_range = s->mac * ((1 << depth) - 1);
  101. s->max_peak_diff = s->mhd * ((1 << depth) - 1);
  102. s->max_period_diff = s->mpd * ((1 << depth) - 1);
  103. s->max_start_diff = s->msd * ((1 << depth) - 1);
  104. s->white = s->wth * ((1 << depth) - 1);
  105. s->black = s->bth * ((1 << depth) - 1);
  106. s->temp = av_calloc(inlink->w, sizeof(*s->temp));
  107. if (!s->temp)
  108. return AVERROR(ENOMEM);
  109. return 0;
  110. }
  111. static void extract_line(AVFilterContext *ctx, AVFilterLink *inlink, AVFrame *in, int line)
  112. {
  113. ReadEIA608Context *s = ctx->priv;
  114. int max = 0, min = INT_MAX;
  115. int i, ch, range = 0;
  116. const uint8_t *src;
  117. uint16_t clock[8][2] = { { 0 } };
  118. const int sync_width = s->spw * in->width;
  119. int last = 0, peaks = 0, max_peak_diff = 0, dir = RISE;
  120. const int width_per_bit = (in->width - sync_width) / 19;
  121. uint8_t byte[2] = { 0 };
  122. int s1, s2, s3, parity;
  123. src = &in->data[0][line * in->linesize[0]];
  124. if (s->lp) {
  125. uint8_t *dst = s->temp;
  126. int w = inlink->w - 1;
  127. for (i = 0; i < inlink->w; i++) {
  128. int a = FFMAX(i - 3, 0);
  129. int b = FFMAX(i - 2, 0);
  130. int c = FFMAX(i - 1, 0);
  131. int d = FFMIN(i + 3, w);
  132. int e = FFMIN(i + 2, w);
  133. int f = FFMIN(i + 1, w);
  134. dst[i] = (src[a] + src[b] + src[c] + src[i] + src[d] + src[e] + src[f] + 6) / 7;
  135. }
  136. src = s->temp;
  137. }
  138. for (i = 0; i < sync_width; i++) {
  139. max = FFMAX(max, src[i]);
  140. min = FFMIN(min, src[i]);
  141. }
  142. range = max - min;
  143. if (range < s->min_range)
  144. return;
  145. for (i = 0; i < sync_width; i++) {
  146. int Y = src[i];
  147. if (dir == RISE) {
  148. if (Y < last) {
  149. dir = FALL;
  150. if (last >= s->white) {
  151. clock[peaks][0] = last;
  152. clock[peaks][1] = i;
  153. peaks++;
  154. if (peaks > 7)
  155. break;
  156. }
  157. }
  158. } else if (dir == FALL) {
  159. if (Y > last && last <= s->black) {
  160. dir = RISE;
  161. }
  162. }
  163. last = Y;
  164. }
  165. if (peaks != 7) {
  166. av_log(ctx, AV_LOG_DEBUG, "peaks: %d != 7\n", peaks);
  167. return;
  168. }
  169. for (i = 1; i < 7; i++)
  170. max_peak_diff = FFMAX(max_peak_diff, FFABS(clock[i][0] - clock[i-1][0]));
  171. if (max_peak_diff > s->max_peak_diff) {
  172. av_log(ctx, AV_LOG_DEBUG, "mhd: %d > %d\n", max_peak_diff, s->max_peak_diff);
  173. return;
  174. }
  175. max = 0; min = INT_MAX;
  176. for (i = 1; i < 7; i++) {
  177. max = FFMAX(max, FFABS(clock[i][1] - clock[i-1][1]));
  178. min = FFMIN(min, FFABS(clock[i][1] - clock[i-1][1]));
  179. }
  180. range = max - min;
  181. if (range > s->max_period_diff) {
  182. av_log(ctx, AV_LOG_DEBUG, "mpd: %d > %d\n", range, s->max_period_diff);
  183. return;
  184. }
  185. s1 = src[sync_width + width_per_bit * 0 + width_per_bit / 2];
  186. s2 = src[sync_width + width_per_bit * 1 + width_per_bit / 2];
  187. s3 = src[sync_width + width_per_bit * 2 + width_per_bit / 2];
  188. if (FFABS(s1 - s2) > s->max_start_diff || s1 > s->black || s2 > s->black || s3 < s->white) {
  189. av_log(ctx, AV_LOG_DEBUG, "msd: %d > %d\n", FFABS(s1 - s2), s->max_start_diff);
  190. return;
  191. }
  192. for (ch = 0; ch < 2; ch++) {
  193. for (parity = 0, i = 0; i < 8; i++) {
  194. int b = src[sync_width + width_per_bit * (i + 3 + 8 * ch) + width_per_bit / 2];
  195. if (b - s1 > (s3 - s1) * s->bhd) {
  196. b = 1;
  197. parity++;
  198. } else {
  199. b = 0;
  200. }
  201. byte[ch] |= b << i;
  202. }
  203. if (s->chp) {
  204. if (!(parity & 1)) {
  205. byte[ch] = 0;
  206. }
  207. }
  208. }
  209. {
  210. uint8_t key[128], value[128];
  211. snprintf(key, sizeof(key), "lavfi.readeia608.%d.cc", s->nb_found);
  212. snprintf(value, sizeof(value), "0x%02X%02X", byte[0], byte[1]);
  213. av_dict_set(&in->metadata, key, value, 0);
  214. snprintf(key, sizeof(key), "lavfi.readeia608.%d.line", s->nb_found);
  215. snprintf(value, sizeof(value), "%d", line);
  216. av_dict_set(&in->metadata, key, value, 0);
  217. }
  218. s->nb_found++;
  219. }
  220. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  221. {
  222. AVFilterContext *ctx = inlink->dst;
  223. AVFilterLink *outlink = ctx->outputs[0];
  224. ReadEIA608Context *s = ctx->priv;
  225. int i;
  226. s->nb_found = 0;
  227. for (i = s->start; i <= s->end; i++)
  228. extract_line(ctx, inlink, in, i);
  229. return ff_filter_frame(outlink, in);
  230. }
  231. static av_cold void uninit(AVFilterContext *ctx)
  232. {
  233. ReadEIA608Context *s = ctx->priv;
  234. av_freep(&s->temp);
  235. }
  236. static const AVFilterPad readeia608_inputs[] = {
  237. {
  238. .name = "default",
  239. .type = AVMEDIA_TYPE_VIDEO,
  240. .filter_frame = filter_frame,
  241. .config_props = config_input,
  242. },
  243. { NULL }
  244. };
  245. static const AVFilterPad readeia608_outputs[] = {
  246. {
  247. .name = "default",
  248. .type = AVMEDIA_TYPE_VIDEO,
  249. },
  250. { NULL }
  251. };
  252. AVFilter ff_vf_readeia608 = {
  253. .name = "readeia608",
  254. .description = NULL_IF_CONFIG_SMALL("Read EIA-608 Closed Caption codes from input video and write them to frame metadata."),
  255. .priv_size = sizeof(ReadEIA608Context),
  256. .priv_class = &readeia608_class,
  257. .query_formats = query_formats,
  258. .inputs = readeia608_inputs,
  259. .outputs = readeia608_outputs,
  260. .uninit = uninit,
  261. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  262. };