1 2 3 4 5 6 7 8 9 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
|
/*
Copyright (C) 2018 Rocky Bernstein <rocky@gnu.org>
Copyright (C) 2000, 2004, 2005 Herbert Valerio Riedel <hvr@gnu.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <cdio/cdio.h>
#include <cdio/bytesex.h>
#include <cdio/util.h>
#include <libvcd/logging.h>
/* Private headers */
#include "vcd_assert.h"
#include "mpeg_stream.h"
#include "data_structures.h"
#include "mpeg.h"
#include "util.h"
struct _VcdMpegSource
{
VcdDataSource_t *data_source;
bool scanned;
/* _get_packet cache */
unsigned _read_pkt_pos;
unsigned _read_pkt_no;
struct vcd_mpeg_stream_info info;
};
/*
* access functions
*/
VcdMpegSource_t *
vcd_mpeg_source_new (VcdDataSource_t *mpeg_file)
{
VcdMpegSource_t *new_obj;
vcd_assert (mpeg_file != NULL);
new_obj = calloc(1, sizeof (VcdMpegSource_t));
new_obj->data_source = mpeg_file;
new_obj->scanned = false;
return new_obj;
}
void
vcd_mpeg_source_destroy (VcdMpegSource_t *obj, bool destroy_file_obj)
{
int i;
vcd_assert (obj != NULL);
if (destroy_file_obj)
vcd_data_source_destroy (obj->data_source);
for (i = 0; i < 3; i++)
if (obj->info.shdr[i].aps_list)
_cdio_list_free (obj->info.shdr[i].aps_list, true, NULL);
free (obj);
}
const struct vcd_mpeg_stream_info *
vcd_mpeg_source_get_info (VcdMpegSource_t *obj)
{
vcd_assert (obj != NULL);
vcd_assert (obj->scanned);
return &(obj->info);
}
long
vcd_mpeg_source_stat (VcdMpegSource_t *obj)
{
vcd_assert (obj != NULL);
vcd_assert (!obj->scanned);
return obj->info.packets * 2324;
}
void
vcd_mpeg_source_scan (VcdMpegSource_t *obj, bool strict_aps, bool fix_scan_info,
vcd_mpeg_prog_cb_t callback, void *user_data)
{
unsigned length = 0;
unsigned pos = 0;
unsigned pno = 0;
unsigned padbytes = 0;
unsigned padpackets = 0;
VcdMpegStreamCtx state;
CdioListNode_t *n;
vcd_mpeg_prog_info_t _progress = { 0, };
vcd_assert (obj != NULL);
if (obj->scanned)
{
vcd_debug ("already scanned... not rescanning");
return;
}
vcd_assert (!obj->scanned);
memset (&state, 0, sizeof (state));
if (fix_scan_info)
state.stream.scan_data_warnings = VCD_MPEG_SCAN_DATA_WARNS + 1;
vcd_data_source_seek (obj->data_source, 0);
length = vcd_data_source_stat (obj->data_source);
if (callback)
{
_progress.length = length;
callback (&_progress, user_data);
}
while (pos < length)
{
char buf[2324] = { 0, };
int read_len = MIN (sizeof (buf), (length - pos));
int pkt_len;
read_len = vcd_data_source_read (obj->data_source, buf, read_len, 1);
pkt_len = vcd_mpeg_parse_packet (buf, read_len, true, &state);
if (!pkt_len)
{
if (!pno)
vcd_error ("input mpeg stream has been deemed invalid -- aborting");
vcd_warn ("bad packet at packet #%d (stream byte offset %d)"
" -- remaining %d bytes of stream will be ignored",
pno, pos, length - pos);
pos = length; /* don't fall into assert... */
break;
}
if (callback && (pos - _progress.current_pos) > (length / 100))
{
_progress.current_pos = pos;
_progress.current_pack = pno;
callback (&_progress, user_data);
}
switch (state.packet.aps)
{
case APS_NONE:
break;
case APS_I:
case APS_GI:
if (strict_aps)
break; /* allow only if now strict aps */
case APS_SGI:
case APS_ASGI:
{
struct aps_data *_data = calloc(1, sizeof (struct aps_data));
_data->packet_no = pno;
_data->timestamp = state.packet.aps_pts;
if (!state.stream.shdr[state.packet.aps_idx].aps_list)
state.stream.shdr[state.packet.aps_idx].aps_list =
_cdio_list_new ();
_cdio_list_append (state.stream.shdr[state.packet.aps_idx].aps_list, _data);
}
break;
default:
vcd_assert_not_reached ();
break;
}
pos += pkt_len;
pno++;
if (pkt_len != read_len)
{
padbytes += (2324 - pkt_len);
if (!padpackets)
vcd_warn ("mpeg stream will be padded on the fly -- hope that's ok for you!");
padpackets++;
vcd_data_source_seek (obj->data_source, pos);
}
}
vcd_data_source_close (obj->data_source);
if (callback)
{
_progress.current_pos = pos;
_progress.current_pack = pno;
callback (&_progress, user_data);
}
vcd_assert (pos == length);
obj->info = state.stream;
obj->scanned = true;
obj->info.playing_time = obj->info.max_pts - obj->info.min_pts;
if (obj->info.min_pts)
vcd_debug ("pts start offset %f (max pts = %f)",
obj->info.min_pts, obj->info.max_pts);
vcd_debug ("playing time %f", obj->info.playing_time);
if (!state.stream.scan_data && state.stream.version == MPEG_VERS_MPEG2)
vcd_warn ("mpeg stream contained no scan information (user) data");
{
int i;
for (i = 0; i < 3; i++)
if (obj->info.shdr[i].aps_list)
_CDIO_LIST_FOREACH (n, obj->info.shdr[i].aps_list)
{
struct aps_data *_data = _cdio_list_node_data (n);
_data->timestamp -= obj->info.min_pts;
}
}
if (padpackets)
vcd_warn ("autopadding requires to insert additional %d zero bytes"
" into MPEG stream (due to %d unaligned packets of %d total)",
padbytes, padpackets, state.stream.packets);
obj->info.version = state.stream.version;
}
static double
_approx_pts (CdioList_t *aps_list, uint32_t packet_no)
{
double retval = 0;
CdioListNode_t *node;
struct aps_data *_laps = NULL;
double last_pts_ratio = 0;
_CDIO_LIST_FOREACH (node, aps_list)
{
struct aps_data *_aps = _cdio_list_node_data (node);
if (_laps)
{
long p = _aps->packet_no;
double t = _aps->timestamp;
p -= _laps->packet_no;
t -= _laps->timestamp;
last_pts_ratio = t / p;
}
if (_aps->packet_no >= packet_no)
break;
_laps = _aps;
}
retval = packet_no;
retval -= _laps->packet_no;
retval *= last_pts_ratio;
retval += _laps->timestamp;
return retval;
}
static void
_set_scan_msf (msf_t *_msf, long lsn)
{
if (lsn == -1)
{
_msf->m = _msf->s = _msf->f = 0xff;
return;
}
cdio_lba_to_msf (lsn, _msf);
_msf->s |= 0x80;
_msf->f |= 0x80;
}
static void
_fix_scan_info (struct vcd_mpeg_scan_data_t *scan_data_ptr,
unsigned packet_no, double pts, CdioList_t *aps_list)
{
CdioListNode_t *node;
long _next = -1, _prev = -1, _forw = -1, _back = -1;
_CDIO_LIST_FOREACH (node, aps_list)
{
struct aps_data *_aps = _cdio_list_node_data (node);
if (_aps->packet_no == packet_no)
continue;
else if (_aps->packet_no < packet_no)
{
_prev = _aps->packet_no;
if (pts - _aps->timestamp < 10 && _back == -1)
_back = _aps->packet_no;
}
else if (_aps->packet_no > packet_no)
{
if (_next == -1)
_next = _aps->packet_no;
if (_aps->timestamp - pts < 10)
_forw = _aps->packet_no;
}
}
if (_back == -1)
_back = packet_no;
if (_forw == -1)
_forw = packet_no;
_set_scan_msf (&scan_data_ptr->prev_ofs, _prev);
_set_scan_msf (&scan_data_ptr->next_ofs, _next);
_set_scan_msf (&scan_data_ptr->back_ofs, _back);
_set_scan_msf (&scan_data_ptr->forw_ofs, _forw);
}
int
vcd_mpeg_source_get_packet (VcdMpegSource_t *obj, unsigned long packet_no,
void *packet_buf,
struct vcd_mpeg_packet_info *flags,
bool fix_scan_info)
{
unsigned length;
unsigned pos;
unsigned pno;
VcdMpegStreamCtx state;
vcd_assert (obj != NULL);
vcd_assert (obj->scanned);
vcd_assert (packet_buf != NULL);
if (packet_no >= obj->info.packets)
{
vcd_error ("invalid argument");
return -1;
}
if (packet_no < obj->_read_pkt_no)
{
vcd_warn ("rewinding mpeg stream...");
obj->_read_pkt_no = 0;
obj->_read_pkt_pos = 0;
}
memset (&state, 0, sizeof (state));
state.stream.seen_pts = true;
state.stream.min_pts = obj->info.min_pts;
state.stream.scan_data_warnings = VCD_MPEG_SCAN_DATA_WARNS + 1;
pos = obj->_read_pkt_pos;
pno = obj->_read_pkt_no;
length = vcd_data_source_stat (obj->data_source);
vcd_data_source_seek (obj->data_source, pos);
while (pos < length)
{
char buf[2324] = { 0, };
int read_len = MIN (sizeof (buf), (length - pos));
int pkt_len;
vcd_data_source_read (obj->data_source, buf, read_len, 1);
pkt_len = vcd_mpeg_parse_packet (buf, read_len,
fix_scan_info, &state);
vcd_assert (pkt_len > 0);
if (pno == packet_no)
{
/* optimized for sequential access,
thus save pointer to next mpeg pack */
obj->_read_pkt_pos = pos + pkt_len;
obj->_read_pkt_no = pno + 1;
if (fix_scan_info
&& state.packet.scan_data_ptr
&& obj->info.version == MPEG_VERS_MPEG2)
{
int vid_idx = 0;
double _pts;
if (state.packet.video[2])
vid_idx = 2;
else if (state.packet.video[1])
vid_idx = 1;
else
vid_idx = 0;
if (state.packet.has_pts)
_pts = state.packet.pts - obj->info.min_pts;
else
_pts = _approx_pts (obj->info.shdr[vid_idx].aps_list,
packet_no);
_fix_scan_info (state.packet.scan_data_ptr, packet_no,
_pts, obj->info.shdr[vid_idx].aps_list);
}
memset (packet_buf, 0, 2324);
memcpy (packet_buf, buf, pkt_len);
if (flags)
{
*flags = state.packet;
flags->pts -= obj->info.min_pts;
}
return 0; /* breaking out */
}
pos += pkt_len;
pno++;
if (pkt_len != read_len)
vcd_data_source_seek (obj->data_source, pos);
}
vcd_assert (pos == length);
vcd_error ("shouldn't be reached...");
return -1;
}
void
vcd_mpeg_source_close (VcdMpegSource_t *p_vcdmpegsource)
{
vcd_assert (p_vcdmpegsource != NULL);
vcd_data_source_close (p_vcdmpegsource->data_source);
}
/*
* Local variables:
* c-file-style: "gnu"
* tab-width: 8
* indent-tabs-mode: nil
* End:
*/
|