You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

444 lines
12KB

  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 LAG 25
  35. #define CLOCK_BITSIZE_MIN 0.2f
  36. #define CLOCK_BITSIZE_MAX 1.5f
  37. #define SYNC_BITSIZE_MIN 12.f
  38. #define SYNC_BITSIZE_MAX 15.f
  39. typedef struct LineItem {
  40. int input;
  41. int output;
  42. float unfiltered;
  43. float filtered;
  44. float average;
  45. float deviation;
  46. } LineItem;
  47. typedef struct CodeItem {
  48. uint8_t bit;
  49. int size;
  50. } CodeItem;
  51. typedef struct ReadEIA608Context {
  52. const AVClass *class;
  53. int start, end;
  54. int nb_found;
  55. int white;
  56. int black;
  57. float spw;
  58. int chp;
  59. int lp;
  60. uint64_t histogram[256];
  61. CodeItem *code;
  62. LineItem *line;
  63. } ReadEIA608Context;
  64. #define OFFSET(x) offsetof(ReadEIA608Context, x)
  65. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  66. static const AVOption readeia608_options[] = {
  67. { "scan_min", "set from which line to scan for codes", OFFSET(start), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
  68. { "scan_max", "set to which line to scan for codes", OFFSET(end), AV_OPT_TYPE_INT, {.i64=29}, 0, INT_MAX, FLAGS },
  69. { "spw", "set ratio of width reserved for sync code detection", OFFSET(spw), AV_OPT_TYPE_FLOAT, {.dbl=.27}, 0.1, 0.7, FLAGS },
  70. { "chp", "check and apply parity bit", OFFSET(chp), AV_OPT_TYPE_BOOL, {.i64= 0}, 0, 1, FLAGS },
  71. { "lp", "lowpass line prior to processing", OFFSET(lp), AV_OPT_TYPE_BOOL, {.i64= 1}, 0, 1, FLAGS },
  72. { NULL }
  73. };
  74. AVFILTER_DEFINE_CLASS(readeia608);
  75. static int query_formats(AVFilterContext *ctx)
  76. {
  77. static const enum AVPixelFormat pixel_fmts[] = {
  78. AV_PIX_FMT_GRAY8,
  79. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  80. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  81. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  82. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  83. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  84. AV_PIX_FMT_YUVJ411P,
  85. AV_PIX_FMT_NONE
  86. };
  87. AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
  88. if (!formats)
  89. return AVERROR(ENOMEM);
  90. return ff_set_common_formats(ctx, formats);
  91. }
  92. static int config_input(AVFilterLink *inlink)
  93. {
  94. AVFilterContext *ctx = inlink->dst;
  95. ReadEIA608Context *s = ctx->priv;
  96. int size = inlink->w + LAG;
  97. if (s->end >= inlink->h) {
  98. av_log(ctx, AV_LOG_WARNING, "Last line to scan too large, clipping.\n");
  99. s->end = inlink->h - 1;
  100. }
  101. if (s->start > s->end) {
  102. av_log(ctx, AV_LOG_ERROR, "Invalid range.\n");
  103. return AVERROR(EINVAL);
  104. }
  105. s->line = av_calloc(size, sizeof(*s->line));
  106. s->code = av_calloc(size, sizeof(*s->code));
  107. if (!s->line || !s->code)
  108. return AVERROR(ENOMEM);
  109. return 0;
  110. }
  111. static void build_histogram(ReadEIA608Context *s, const LineItem *line, int len)
  112. {
  113. memset(s->histogram, 0, sizeof(s->histogram));
  114. for (int i = LAG; i < len + LAG; i++)
  115. s->histogram[line[i].input]++;
  116. }
  117. static void find_black_and_white(ReadEIA608Context *s)
  118. {
  119. int start = 0, end = 0, middle;
  120. int black = 0, white = 0;
  121. int cnt;
  122. for (int i = 0; i < 256; i++) {
  123. if (s->histogram[i]) {
  124. start = i;
  125. break;
  126. }
  127. }
  128. for (int i = 255; i >= 0; i--) {
  129. if (s->histogram[i]) {
  130. end = i;
  131. break;
  132. }
  133. }
  134. middle = start + (end - start) / 2;
  135. cnt = 0;
  136. for (int i = start; i <= middle; i++) {
  137. if (s->histogram[i] > cnt) {
  138. cnt = s->histogram[i];
  139. black = i;
  140. }
  141. }
  142. cnt = 0;
  143. for (int i = end; i >= middle; i--) {
  144. if (s->histogram[i] > cnt) {
  145. cnt = s->histogram[i];
  146. white = i;
  147. }
  148. }
  149. s->black = black;
  150. s->white = white;
  151. }
  152. static float meanf(const LineItem *line, int len)
  153. {
  154. float sum = 0.0, mean = 0.0;
  155. for (int i = 0; i < len; i++)
  156. sum += line[i].filtered;
  157. mean = sum / len;
  158. return mean;
  159. }
  160. static float stddevf(const LineItem *line, int len)
  161. {
  162. float m = meanf(line, len);
  163. float standard_deviation = 0.f;
  164. for (int i = 0; i < len; i++)
  165. standard_deviation += (line[i].filtered - m) * (line[i].filtered - m);
  166. return sqrtf(standard_deviation / (len - 1));
  167. }
  168. static void thresholding(ReadEIA608Context *s, LineItem *line,
  169. int lag, float threshold, float influence, int len)
  170. {
  171. for (int i = lag; i < len + lag; i++) {
  172. line[i].unfiltered = line[i].input / 255.f;
  173. line[i].filtered = line[i].unfiltered;
  174. }
  175. for (int i = 0; i < lag; i++) {
  176. line[i].unfiltered = meanf(line, len * s->spw);
  177. line[i].filtered = line[i].unfiltered;
  178. }
  179. line[lag - 1].average = meanf(line, lag);
  180. line[lag - 1].deviation = stddevf(line, lag);
  181. for (int i = lag; i < len + lag; i++) {
  182. if (fabsf(line[i].unfiltered - line[i-1].average) > threshold * line[i-1].deviation) {
  183. if (line[i].unfiltered > line[i-1].average) {
  184. line[i].output = 255;
  185. } else {
  186. line[i].output = 0;
  187. }
  188. line[i].filtered = influence * line[i].unfiltered + (1.f - influence) * line[i-1].filtered;
  189. } else {
  190. int distance_from_black, distance_from_white;
  191. distance_from_black = FFABS(line[i].input - s->black);
  192. distance_from_white = FFABS(line[i].input - s->white);
  193. line[i].output = distance_from_black <= distance_from_white ? 0 : 255;
  194. }
  195. line[i].average = meanf(line + i - lag, lag);
  196. line[i].deviation = stddevf(line + i - lag, lag);
  197. }
  198. }
  199. static int periods(const LineItem *line, CodeItem *code, int len)
  200. {
  201. int hold = line[LAG].output, cnt = 0;
  202. int last = LAG;
  203. memset(code, 0, len * sizeof(*code));
  204. for (int i = LAG + 1; i < len + LAG; i++) {
  205. if (line[i].output != hold) {
  206. code[cnt].size = i - last;
  207. code[cnt].bit = hold;
  208. hold = line[i].output;
  209. last = i;
  210. cnt++;
  211. }
  212. }
  213. code[cnt].size = LAG + len - last;
  214. code[cnt].bit = hold;
  215. return cnt + 1;
  216. }
  217. static void dump_code(AVFilterContext *ctx, int len, int item)
  218. {
  219. ReadEIA608Context *s = ctx->priv;
  220. av_log(ctx, AV_LOG_DEBUG, "%d:", item);
  221. for (int i = 0; i < len; i++) {
  222. av_log(ctx, AV_LOG_DEBUG, " %03d", s->code[i].size);
  223. }
  224. av_log(ctx, AV_LOG_DEBUG, "\n");
  225. }
  226. static void extract_line(AVFilterContext *ctx, AVFrame *in, int w, int nb_line)
  227. {
  228. ReadEIA608Context *s = ctx->priv;
  229. LineItem *line = s->line;
  230. int i, j, ch, len;
  231. const uint8_t *src;
  232. uint8_t byte[2] = { 0 };
  233. uint8_t codes[19] = { 0 };
  234. float bit_size = 0.f;
  235. int parity;
  236. memset(line, 0, (w + LAG) * sizeof(*line));
  237. src = &in->data[0][nb_line * in->linesize[0]];
  238. if (s->lp) {
  239. for (i = 0; i < w; i++) {
  240. int a = FFMAX(i - 3, 0);
  241. int b = FFMAX(i - 2, 0);
  242. int c = FFMAX(i - 1, 0);
  243. int d = FFMIN(i + 3, w-1);
  244. int e = FFMIN(i + 2, w-1);
  245. int f = FFMIN(i + 1, w-1);
  246. line[LAG + i].input = (src[a] + src[b] + src[c] + src[i] + src[d] + src[e] + src[f] + 6) / 7;
  247. }
  248. } else {
  249. for (i = 0; i < w; i++) {
  250. line[LAG + i].input = src[i];
  251. }
  252. }
  253. build_histogram(s, line, w);
  254. find_black_and_white(s);
  255. if (s->white - s->black < 5)
  256. return;
  257. thresholding(s, line, LAG, 1, 0, w);
  258. len = periods(line, s->code, w);
  259. dump_code(ctx, len, nb_line);
  260. if (len < 15 ||
  261. s->code[14].bit != 0 ||
  262. w / (float)s->code[14].size < SYNC_BITSIZE_MIN ||
  263. w / (float)s->code[14].size > SYNC_BITSIZE_MAX) {
  264. return;
  265. }
  266. for (i = 14; i < len; i++) {
  267. bit_size += s->code[i].size;
  268. }
  269. bit_size /= 19.f;
  270. for (i = 1; i < 14; i++) {
  271. if (s->code[i].size / bit_size > CLOCK_BITSIZE_MAX ||
  272. s->code[i].size / bit_size < CLOCK_BITSIZE_MIN) {
  273. return;
  274. }
  275. }
  276. if (s->code[15].size / bit_size < 0.45f) {
  277. return;
  278. }
  279. for (j = 0, i = 14; i < len; i++) {
  280. int run, bit;
  281. run = lrintf(s->code[i].size / bit_size);
  282. bit = s->code[i].bit;
  283. for (int k = 0; j < 19 && k < run; k++) {
  284. codes[j++] = bit;
  285. }
  286. if (j >= 19)
  287. break;
  288. }
  289. for (ch = 0; ch < 2; ch++) {
  290. for (parity = 0, i = 0; i < 8; i++) {
  291. int b = codes[3 + ch * 8 + i];
  292. if (b == 255) {
  293. parity++;
  294. b = 1;
  295. } else {
  296. b = 0;
  297. }
  298. byte[ch] |= b << i;
  299. }
  300. if (s->chp) {
  301. if (!(parity & 1)) {
  302. byte[ch] = 0x7F;
  303. }
  304. }
  305. }
  306. {
  307. uint8_t key[128], value[128];
  308. //snprintf(key, sizeof(key), "lavfi.readeia608.%d.bits", s->nb_found);
  309. //snprintf(value, sizeof(value), "0b%d%d%d%d%d%d%d%d 0b%d%d%d%d%d%d%d%d", codes[3]==255,codes[4]==255,codes[5]==255,codes[6]==255,codes[7]==255,codes[8]==255,codes[9]==255,codes[10]==255,codes[11]==255,codes[12]==255,codes[13]==255,codes[14]==255,codes[15]==255,codes[16]==255,codes[17]==255,codes[18]==255);
  310. //av_dict_set(&in->metadata, key, value, 0);
  311. snprintf(key, sizeof(key), "lavfi.readeia608.%d.cc", s->nb_found);
  312. snprintf(value, sizeof(value), "0x%02X%02X", byte[0], byte[1]);
  313. av_dict_set(&in->metadata, key, value, 0);
  314. snprintf(key, sizeof(key), "lavfi.readeia608.%d.line", s->nb_found);
  315. snprintf(value, sizeof(value), "%d", nb_line);
  316. av_dict_set(&in->metadata, key, value, 0);
  317. }
  318. s->nb_found++;
  319. }
  320. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  321. {
  322. AVFilterContext *ctx = inlink->dst;
  323. AVFilterLink *outlink = ctx->outputs[0];
  324. ReadEIA608Context *s = ctx->priv;
  325. int i;
  326. s->nb_found = 0;
  327. for (i = s->start; i <= s->end; i++)
  328. extract_line(ctx, in, inlink->w, i);
  329. return ff_filter_frame(outlink, in);
  330. }
  331. static av_cold void uninit(AVFilterContext *ctx)
  332. {
  333. ReadEIA608Context *s = ctx->priv;
  334. av_freep(&s->code);
  335. av_freep(&s->line);
  336. }
  337. static const AVFilterPad readeia608_inputs[] = {
  338. {
  339. .name = "default",
  340. .type = AVMEDIA_TYPE_VIDEO,
  341. .filter_frame = filter_frame,
  342. .config_props = config_input,
  343. },
  344. { NULL }
  345. };
  346. static const AVFilterPad readeia608_outputs[] = {
  347. {
  348. .name = "default",
  349. .type = AVMEDIA_TYPE_VIDEO,
  350. },
  351. { NULL }
  352. };
  353. AVFilter ff_vf_readeia608 = {
  354. .name = "readeia608",
  355. .description = NULL_IF_CONFIG_SMALL("Read EIA-608 Closed Caption codes from input video and write them to frame metadata."),
  356. .priv_size = sizeof(ReadEIA608Context),
  357. .priv_class = &readeia608_class,
  358. .query_formats = query_formats,
  359. .inputs = readeia608_inputs,
  360. .outputs = readeia608_outputs,
  361. .uninit = uninit,
  362. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  363. };