| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
> |
| 9 |
om> |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
#ifdef HAVE_CONFIG_H |
| 52 |
#include <config.h> |
| 53 |
#endif |
| 54 |
|
| 55 |
#include "gstaccurip.h" |
| 56 |
|
| 57 |
#define DEFAULT_MAX_DURATION 120 |
| 58 |
|
| 59 |
#define PAD_CAPS \ |
| 60 |
"audio/x-raw, " \ |
| 61 |
"format = (string) " GST_AUDIO_NE(S16) ", "\ |
| 62 |
"rate = (int) 44100, " \ |
| 63 |
"channels = (int) 2" |
| 64 |
|
| 65 |
GST_DEBUG_CATEGORY_STATIC (gst_accurip_debug); |
| 66 |
#define GST_CAT_DEFAULT gst_accurip_debug |
| 67 |
|
| 68 |
enum |
| 69 |
{ |
| 70 |
PROP_0, |
| 71 |
PROP_FIRST_TRACK, |
| 72 |
PROP_LAST_TRACK |
| 73 |
}; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
static void gst_accurip_finalize (GObject * object); |
| 79 |
static void gst_accurip_set_property (GObject * object, guint prop_id, |
| 80 |
const GValue * value, GParamSpec * pspec); |
| 81 |
static void gst_accurip_get_property (GObject * object, guint prop_id, |
| 82 |
GValue * value, GParamSpec * pspec); |
| 83 |
static GstFlowReturn gst_accurip_transform_ip (GstBaseTransform * trans, |
| 84 |
GstBuffer * buf); |
| 85 |
static gboolean gst_accurip_sink_event (GstBaseTransform * trans, |
| 86 |
GstEvent * event); |
| 87 |
static gboolean accurip_element_init (GstPlugin * plugin); |
| 88 |
|
| 89 |
#define parent_class gst_accurip_parent_class |
| 90 |
G_DEFINE_TYPE (GstAccurip, gst_accurip, GST_TYPE_AUDIO_FILTER); |
| 91 |
GST_ELEMENT_REGISTER_DEFINE_CUSTOM (accurip, accurip_element_init); |
| 92 |
|
| 93 |
static void |
| 94 |
gst_accurip_class_init (GstAccuripClass * klass) |
| 95 |
{ |
| 96 |
GObjectClass *gobject_class; |
| 97 |
GstBaseTransformClass *gstbasetrans_class; |
| 98 |
GstCaps *caps; |
| 99 |
|
| 100 |
gobject_class = G_OBJECT_CLASS (klass); |
| 101 |
gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass); |
| 102 |
|
| 103 |
gobject_class->set_property = gst_accurip_set_property; |
| 104 |
gobject_class->get_property = gst_accurip_get_property; |
| 105 |
|
| 106 |
g_object_class_install_property (gobject_class, PROP_FIRST_TRACK, |
| 107 |
g_param_spec_boolean ("first-track", "First track", |
| 108 |
"Indicate to the CRC calculation algorithm that this is the first track of a set", |
| 109 |
FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 110 |
|
| 111 |
g_object_class_install_property (gobject_class, PROP_LAST_TRACK, |
| 112 |
g_param_spec_boolean ("last-track", "Last track", |
| 113 |
"Indicate to the CRC calculation algorithm that this is the last track of a set", |
| 114 |
FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 115 |
|
| 116 |
gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_accurip_finalize); |
| 117 |
|
| 118 |
gstbasetrans_class->transform_ip = |
| 119 |
GST_DEBUG_FUNCPTR (gst_accurip_transform_ip); |
| 120 |
gstbasetrans_class->sink_event = GST_DEBUG_FUNCPTR (gst_accurip_sink_event); |
| 121 |
gstbasetrans_class->passthrough_on_same_caps = TRUE; |
| 122 |
|
| 123 |
gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass), |
| 124 |
"AccurateRip(TM) CRC element", |
| 125 |
"Filter/Analyzer/Audio", |
| 126 |
"Computes an AccurateRip CRC", "Christophe Fergeau <[email protected]>"); |
| 127 |
|
| 128 |
caps = gst_caps_from_string (PAD_CAPS); |
| 129 |
gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass), |
| 130 |
caps); |
| 131 |
gst_caps_unref (caps); |
| 132 |
} |
| 133 |
|
| 134 |
static void |
| 135 |
ring_free (GstAccurip * accurip) |
| 136 |
{ |
| 137 |
g_free (accurip->crcs_ring); |
| 138 |
g_free (accurip->crcs_v2_ring); |
| 139 |
accurip->crcs_ring = NULL; |
| 140 |
accurip->crcs_v2_ring = NULL; |
| 141 |
accurip->ring_samples = 0; |
| 142 |
} |
| 143 |
|
| 144 |
static void |
| 145 |
gst_accurip_reset (GstAccurip * accurip) |
| 146 |
{ |
| 147 |
if (accurip->num_samples != 0) { |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
accurip->is_first = FALSE; |
| 153 |
accurip->is_last = FALSE; |
| 154 |
ring_free (accurip); |
| 155 |
} |
| 156 |
accurip->crc = 0; |
| 157 |
accurip->crc_v2 = 0; |
| 158 |
|
| 159 |
accurip->num_samples = 0; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
#define IGNORED_SAMPLES_COUNT (2352 * 5 / (2*2)) |
| 165 |
|
| 166 |
static void |
| 167 |
gst_accurip_emit_tags (GstAccurip * accurip) |
| 168 |
{ |
| 169 |
GstTagList *tags; |
| 170 |
|
| 171 |
if (accurip->num_samples == 0) |
| 172 |
return; |
| 173 |
|
| 174 |
if (accurip->is_last) { |
| 175 |
guint index; |
| 176 |
if (accurip->ring_samples <= IGNORED_SAMPLES_COUNT) { |
| 177 |
return; |
| 178 |
} |
| 179 |
index = accurip->ring_samples - IGNORED_SAMPLES_COUNT; |
| 180 |
index %= (IGNORED_SAMPLES_COUNT + 1); |
| 181 |
accurip->crc = accurip->crcs_ring[index]; |
| 182 |
accurip->crc_v2 = accurip->crcs_v2_ring[index]; |
| 183 |
} |
| 184 |
|
| 185 |
GST_DEBUG_OBJECT (accurip, |
| 186 |
"Generating CRC based on %" G_GUINT64_FORMAT " samples", |
| 187 |
accurip->num_samples); |
| 188 |
|
| 189 |
tags = gst_tag_list_new (GST_TAG_ACCURIP_CRC, accurip->crc, |
| 190 |
GST_TAG_ACCURIP_CRC_V2, accurip->crc_v2, NULL); |
| 191 |
|
| 192 |
GST_DEBUG_OBJECT (accurip, "Computed CRC=%08X and CRCv2=0x%08X", |
| 193 |
accurip->crc, accurip->crc_v2); |
| 194 |
|
| 195 |
gst_pad_push_event (GST_BASE_TRANSFORM_SRC_PAD (accurip), |
| 196 |
gst_event_new_tag (tags)); |
| 197 |
} |
| 198 |
|
| 199 |
static void |
| 200 |
gst_accurip_init (GstAccurip * accurip) |
| 201 |
{ |
| 202 |
gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (accurip), TRUE); |
| 203 |
} |
| 204 |
|
| 205 |
static void |
| 206 |
gst_accurip_finalize (GObject * object) |
| 207 |
{ |
| 208 |
ring_free (GST_ACCURIP (object)); |
| 209 |
|
| 210 |
G_OBJECT_CLASS (parent_class)->finalize (object); |
| 211 |
} |
| 212 |
|
| 213 |
static GstFlowReturn |
| 214 |
gst_accurip_transform_ip (GstBaseTransform * trans, GstBuffer * buf) |
| 215 |
{ |
| 216 |
GstAccurip *accurip = GST_ACCURIP (trans); |
| 217 |
GstAudioFilter *filter = GST_AUDIO_FILTER (trans); |
| 218 |
guint32 *data; |
| 219 |
GstMapInfo map_info; |
| 220 |
guint nsamples; |
| 221 |
gint channels; |
| 222 |
guint i; |
| 223 |
|
| 224 |
channels = GST_AUDIO_INFO_CHANNELS (&filter->info); |
| 225 |
|
| 226 |
if (G_UNLIKELY (channels != 2)) |
| 227 |
return GST_FLOW_NOT_NEGOTIATED; |
| 228 |
|
| 229 |
if (!gst_buffer_map (buf, &map_info, GST_MAP_READ)) |
| 230 |
return GST_FLOW_ERROR; |
| 231 |
|
| 232 |
data = (guint32 *) map_info.data; |
| 233 |
nsamples = map_info.size / (channels * 2); |
| 234 |
|
| 235 |
for (i = 0; i < nsamples; i++) { |
| 236 |
guint64 mult_sample; |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
accurip->num_samples++; |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
if (accurip->is_first && accurip->num_samples < IGNORED_SAMPLES_COUNT) |
| 248 |
continue; |
| 249 |
|
| 250 |
|
| 251 |
mult_sample = data[i] * accurip->num_samples; |
| 252 |
accurip->crc += mult_sample; |
| 253 |
accurip->crc_v2 += mult_sample & 0xffffffff; |
| 254 |
accurip->crc_v2 += (mult_sample >> 32); |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
if (accurip->is_last) { |
| 263 |
guint index = accurip->ring_samples % (IGNORED_SAMPLES_COUNT + 1); |
| 264 |
accurip->ring_samples++; |
| 265 |
accurip->crcs_ring[index] = accurip->crc; |
| 266 |
accurip->crcs_v2_ring[index] = accurip->crc_v2; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
gst_buffer_unmap (buf, &map_info); |
| 271 |
|
| 272 |
return GST_FLOW_OK; |
| 273 |
} |
| 274 |
|
| 275 |
static gboolean |
| 276 |
gst_accurip_sink_event (GstBaseTransform * trans, GstEvent * event) |
| 277 |
{ |
| 278 |
GstAccurip *accurip = GST_ACCURIP (trans); |
| 279 |
|
| 280 |
switch (GST_EVENT_TYPE (event)) { |
| 281 |
case GST_EVENT_FLUSH_STOP: |
| 282 |
case GST_EVENT_SEGMENT: |
| 283 |
GST_DEBUG_OBJECT (trans, "Got %s event, clearing buffer", |
| 284 |
GST_EVENT_TYPE_NAME (event)); |
| 285 |
gst_accurip_emit_tags (accurip); |
| 286 |
gst_accurip_reset (accurip); |
| 287 |
break; |
| 288 |
case GST_EVENT_EOS: |
| 289 |
gst_accurip_emit_tags (accurip); |
| 290 |
break; |
| 291 |
default: |
| 292 |
break; |
| 293 |
} |
| 294 |
|
| 295 |
return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event); |
| 296 |
} |
| 297 |
|
| 298 |
static void |
| 299 |
gst_accurip_set_property (GObject * object, guint prop_id, |
| 300 |
const GValue * value, GParamSpec * pspec) |
| 301 |
{ |
| 302 |
GstAccurip *accurip = GST_ACCURIP (object); |
| 303 |
|
| 304 |
switch (prop_id) { |
| 305 |
case PROP_FIRST_TRACK: |
| 306 |
accurip->is_first = g_value_get_boolean (value); |
| 307 |
break; |
| 308 |
case PROP_LAST_TRACK: |
| 309 |
if (accurip->is_last != g_value_get_boolean (value)) { |
| 310 |
ring_free (accurip); |
| 311 |
} |
| 312 |
accurip->is_last = g_value_get_boolean (value); |
| 313 |
if (accurip->is_last) { |
| 314 |
if (accurip->crcs_ring == NULL) { |
| 315 |
accurip->crcs_ring = g_new0 (guint32, IGNORED_SAMPLES_COUNT + 1); |
| 316 |
} |
| 317 |
if (accurip->crcs_v2_ring == NULL) { |
| 318 |
accurip->crcs_v2_ring = g_new0 (guint32, IGNORED_SAMPLES_COUNT + 1); |
| 319 |
} |
| 320 |
} |
| 321 |
break; |
| 322 |
default: |
| 323 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 324 |
break; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
static void |
| 329 |
gst_accurip_get_property (GObject * object, guint prop_id, |
| 330 |
GValue * value, GParamSpec * pspec) |
| 331 |
{ |
| 332 |
GstAccurip *accurip = GST_ACCURIP (object); |
| 333 |
|
| 334 |
switch (prop_id) { |
| 335 |
case PROP_FIRST_TRACK: |
| 336 |
g_value_set_boolean (value, accurip->is_first); |
| 337 |
break; |
| 338 |
case PROP_LAST_TRACK: |
| 339 |
g_value_set_boolean (value, accurip->is_last); |
| 340 |
break; |
| 341 |
default: |
| 342 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 343 |
break; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
static gboolean |
| 348 |
accurip_element_init (GstPlugin * plugin) |
| 349 |
{ |
| 350 |
gboolean ret; |
| 351 |
|
| 352 |
GST_DEBUG_CATEGORY_INIT (gst_accurip_debug, "accurip", 0, "accurip element"); |
| 353 |
|
| 354 |
ret = gst_element_register (plugin, "accurip", GST_RANK_NONE, |
| 355 |
GST_TYPE_ACCURIP); |
| 356 |
|
| 357 |
if (ret) { |
| 358 |
gst_tag_register (GST_TAG_ACCURIP_CRC, GST_TAG_FLAG_META, |
| 359 |
G_TYPE_UINT, "accurip crc", "AccurateRip(TM) CRC", NULL); |
| 360 |
gst_tag_register (GST_TAG_ACCURIP_CRC_V2, GST_TAG_FLAG_META, |
| 361 |
G_TYPE_UINT, "accurip crc (v2)", "AccurateRip(TM) CRC (version 2)", |
| 362 |
NULL); |
| 363 |
} |
| 364 |
|
| 365 |
return ret; |
| 366 |
} |
| 367 |
|
| 368 |
static gboolean |
| 369 |
plugin_init (GstPlugin * plugin) |
| 370 |
{ |
| 371 |
return GST_ELEMENT_REGISTER (accurip, plugin); |
| 372 |
} |
| 373 |
|
| 374 |
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, |
| 375 |
GST_VERSION_MINOR, |
| 376 |
accurip, |
| 377 |
"Computes an AccurateRip CRC", |
| 378 |
plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN) |