Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2022 Michael Niedermayer | ||
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 | #include "bsf.h" | ||
22 | #include "bsf_internal.h" | ||
23 | #include "libavutil/colorspace.h" | ||
24 | #include "libavutil/intreadwrite.h" | ||
25 | #include "libavutil/opt.h" | ||
26 | |||
27 | typedef struct DVErrorMarkerContext { | ||
28 | const AVClass *class; | ||
29 | uint8_t color_rgba[4]; | ||
30 | int sta; | ||
31 | uint8_t marked_block[76]; | ||
32 | } DVErrorMarkerContext; | ||
33 | |||
34 | 2 | static void setdc(uint8_t *b, const uint8_t color_rgba[4], int cblocks, int y_step, int v_step, int u_step) { | |
35 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
|
10 | for (int i=0; i<4; i++) { |
36 | 8 | b[0] = RGB_TO_Y_JPEG(color_rgba[0], color_rgba[1],color_rgba[2]) + 128; | |
37 | 8 | b[1] = 0x06; | |
38 | 8 | b += y_step; | |
39 | } | ||
40 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
|
5 | for (int i=0; i<cblocks; i++) { |
41 | 3 | b[0] = RGB_TO_V_JPEG(color_rgba[0], color_rgba[1],color_rgba[2]) - 128; | |
42 | 3 | b[1] = 0x16; | |
43 | 3 | b += v_step; | |
44 | } | ||
45 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
|
5 | for (int i=0; i<cblocks; i++) { |
46 | 3 | b[0] = RGB_TO_U_JPEG(color_rgba[0], color_rgba[1],color_rgba[2]) - 128; | |
47 | 3 | b[1] = 0x16; | |
48 | 3 | b += u_step; | |
49 | } | ||
50 | 2 | } | |
51 | |||
52 | 1 | static int dv_error_marker_init(AVBSFContext *ctx) | |
53 | { | ||
54 | 1 | DVErrorMarkerContext *s = ctx->priv_data; | |
55 | |||
56 | 1 | memset(s->marked_block, -1, 76); | |
57 | 1 | setdc(s->marked_block, s->color_rgba, 1, 14, 10, 10); | |
58 | 1 | setdc(s->marked_block, s->color_rgba, 2, 10, 10, 8); | |
59 | |||
60 | 1 | return 0; | |
61 | } | ||
62 | |||
63 | 3 | static int dv_error_marker_filter(AVBSFContext *ctx, AVPacket *pkt) | |
64 | { | ||
65 | 3 | DVErrorMarkerContext *s = ctx->priv_data; | |
66 | 3 | int ret = ff_bsf_get_packet_ref(ctx, pkt); | |
67 | uint8_t *p; | ||
68 | 3 | int writable = 0; | |
69 | 3 | int stamask = s->sta; | |
70 | 3 | int match_count = 0; | |
71 | |||
72 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if (ret < 0) |
73 | 2 | return ret; | |
74 | |||
75 | 1 | p = pkt->data; | |
76 |
2/2✓ Branch 0 taken 3600 times.
✓ Branch 1 taken 1 times.
|
3601 | for(int i = 0; i < pkt->size - 79; i+=80) { |
77 | // see page 44-46 or section 5.5 of http://web.archive.org/web/20060927044735/http://www.smpte.org/smpte_store/standards/pdf/s314m.pdf. | ||
78 |
4/4✓ Branch 0 taken 3212 times.
✓ Branch 1 taken 388 times.
✓ Branch 2 taken 38 times.
✓ Branch 3 taken 3174 times.
|
3600 | if ((p[i] >> 4) == 9 && ((stamask >> (p[i+3] >> 4))&1)) { |
79 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 37 times.
|
38 | if (!writable) { |
80 | 1 | ret = av_packet_make_writable(pkt); | |
81 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) { |
82 | ✗ | av_packet_unref(pkt); | |
83 | ✗ | return ret; | |
84 | } | ||
85 | 1 | writable = 1; | |
86 | 1 | p = pkt->data; | |
87 | } | ||
88 | 38 | memcpy(p+i+4, s->marked_block, 76); | |
89 | 38 | match_count ++; | |
90 | } | ||
91 | } | ||
92 | 1 | av_log(ctx, AV_LOG_DEBUG, "%8"PRId64": Replaced %5d blocks by color %X\n", pkt->pts, match_count, AV_RB32(s->color_rgba)); | |
93 | |||
94 | 1 | return 0; | |
95 | } | ||
96 | |||
97 | #define OFFSET(x) offsetof(DVErrorMarkerContext, x) | ||
98 | #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM) | ||
99 | static const AVOption options[] = { | ||
100 | { "color" , "set color", OFFSET(color_rgba), AV_OPT_TYPE_COLOR, {.str = "yellow"}, 0, 0, FLAGS }, | ||
101 | { "sta" , "specify which error status value to match" | ||
102 | , OFFSET(sta ), AV_OPT_TYPE_FLAGS, {.i64 = 0xFFFE}, 0, 0xFFFF, FLAGS, .unit = "sta" }, | ||
103 | { "ok" , "No error, no concealment", 0, AV_OPT_TYPE_CONST, {.i64 = 0x0001}, 0, 0xFFFF, FLAGS, .unit = "sta"}, | ||
104 | { "Aa" , "No error, concealment from previous frame type a",0, AV_OPT_TYPE_CONST, {.i64 = 0x0004}, 0, 0xFFFF, FLAGS, .unit = "sta"}, | ||
105 | { "Ba" , "No error, concealment from next frame type a", 0, AV_OPT_TYPE_CONST, {.i64 = 0x0010}, 0, 0xFFFF, FLAGS, .unit = "sta"}, | ||
106 | { "Ca" , "No error, unspecified concealment type a", 0, AV_OPT_TYPE_CONST, {.i64 = 0x0040}, 0, 0xFFFF, FLAGS, .unit = "sta"}, | ||
107 | { "erri" , "Error with inserted code, No concealment", 0, AV_OPT_TYPE_CONST, {.i64 = 0x0080}, 0, 0xFFFF, FLAGS, .unit = "sta"}, | ||
108 | { "erru" , "Error with unidentified pos, No concealment", 0, AV_OPT_TYPE_CONST, {.i64 = 0x8000}, 0, 0xFFFF, FLAGS, .unit = "sta"}, | ||
109 | { "err" , "Error, No concealment", 0, AV_OPT_TYPE_CONST, {.i64 = 0x8080}, 0, 0xFFFF, FLAGS, .unit = "sta"}, | ||
110 | { "Ab" , "No error, concealment from previous frame type b",0, AV_OPT_TYPE_CONST, {.i64 = 0x0400}, 0, 0xFFFF, FLAGS, .unit = "sta"}, | ||
111 | { "Bb" , "No error, concealment from next frame type b", 0, AV_OPT_TYPE_CONST, {.i64 = 0x1000}, 0, 0xFFFF, FLAGS, .unit = "sta"}, | ||
112 | { "Cb" , "No error, unspecified concealment type b", 0, AV_OPT_TYPE_CONST, {.i64 = 0x4000}, 0, 0xFFFF, FLAGS, .unit = "sta"}, | ||
113 | { "A" , "No error, concealment from previous frame", 0, AV_OPT_TYPE_CONST, {.i64 = 0x0404}, 0, 0xFFFF, FLAGS, .unit = "sta"}, | ||
114 | { "B" , "No error, concealment from next frame", 0, AV_OPT_TYPE_CONST, {.i64 = 0x1010}, 0, 0xFFFF, FLAGS, .unit = "sta"}, | ||
115 | { "C" , "No error, unspecified concealment", 0, AV_OPT_TYPE_CONST, {.i64 = 0x4040}, 0, 0xFFFF, FLAGS, .unit = "sta"}, | ||
116 | { "a" , "No error, concealment type a", 0, AV_OPT_TYPE_CONST, {.i64 = 0x0054}, 0, 0xFFFF, FLAGS, .unit = "sta"}, | ||
117 | { "b" , "No error, concealment type b", 0, AV_OPT_TYPE_CONST, {.i64 = 0x5400}, 0, 0xFFFF, FLAGS, .unit = "sta"}, | ||
118 | { "res" , "Reserved", 0, AV_OPT_TYPE_CONST, {.i64 = 0x2B2A}, 0, 0xFFFF, FLAGS, .unit = "sta"}, | ||
119 | { "notok" , "Error or concealment", 0, AV_OPT_TYPE_CONST, {.i64 = 0xD4D4}, 0, 0xFFFF, FLAGS, .unit = "sta"}, | ||
120 | { "notres" , "Not reserved", 0, AV_OPT_TYPE_CONST, {.i64 = 0xD4D5}, 0, 0xFFFF, FLAGS, .unit = "sta"}, | ||
121 | { NULL }, | ||
122 | }; | ||
123 | |||
124 | static const AVClass dv_error_marker_class = { | ||
125 | .class_name = "dv_error_marker", | ||
126 | .item_name = av_default_item_name, | ||
127 | .option = options, | ||
128 | .version = LIBAVUTIL_VERSION_INT, | ||
129 | }; | ||
130 | |||
131 | const FFBitStreamFilter ff_dv_error_marker_bsf = { | ||
132 | .p.name = "dv_error_marker", | ||
133 | .p.codec_ids = (const enum AVCodecID []){ AV_CODEC_ID_DVVIDEO, AV_CODEC_ID_NONE }, | ||
134 | .p.priv_class = &dv_error_marker_class, | ||
135 | .priv_data_size = sizeof(DVErrorMarkerContext), | ||
136 | .init = dv_error_marker_init, | ||
137 | .filter = dv_error_marker_filter, | ||
138 | }; | ||
139 |