- if(!best_st)
- return -1;
-
- best_ast = best_st->priv_data;
- best_ts = av_rescale_q(best_ts, (AVRational){FFMAX(1, best_ast->sample_size), AV_TIME_BASE}, best_st->time_base);
- if(best_ast->remaining)
- i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
- else{
- i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
- if(i>=0)
- best_ast->frame_offset= best_st->index_entries[i].timestamp;
+
+ if (avi->dv_demux && n != 0)
+ continue;
+
+ // parse ##dc/##wb
+ if (n < s->nb_streams) {
+ AVStream *st;
+ AVIStream *ast;
+ st = s->streams[n];
+ ast = st->priv_data;
+
+ if (s->nb_streams >= 2) {
+ AVStream *st1 = s->streams[1];
+ AVIStream *ast1 = st1->priv_data;
+ // workaround for broken small-file-bug402.avi
+ if (d[2] == 'w' && d[3] == 'b' && n == 0 &&
+ st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
+ st1->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
+ ast->prefix == 'd' * 256 + 'c' &&
+ (d[2] * 256 + d[3] == ast1->prefix ||
+ !ast1->prefix_count)) {
+ n = 1;
+ st = st1;
+ ast = ast1;
+ av_log(s, AV_LOG_WARNING,
+ "Invalid stream + prefix combination, assuming audio.\n");
+ }
+ }
+
+ if (!avi->dv_demux &&
+ ((st->discard >= AVDISCARD_DEFAULT && size == 0) /* ||
+ // FIXME: needs a little reordering
+ (st->discard >= AVDISCARD_NONKEY &&
+ !(pkt->flags & AV_PKT_FLAG_KEY)) */
+ || st->discard >= AVDISCARD_ALL)) {
+ if (!exit_early) {
+ ast->frame_offset += get_duration(ast, size);
+ }
+ avio_skip(pb, size);
+ goto start_sync;
+ }
+
+ if (d[2] == 'p' && d[3] == 'c' && size <= 4 * 256 + 4) {
+ int k = avio_r8(pb);
+ int last = (k + avio_r8(pb) - 1) & 0xFF;
+
+ avio_rl16(pb); // flags
+
+ // b + (g << 8) + (r << 16);
+ for (; k <= last; k++)
+ ast->pal[k] = avio_rb32(pb) >> 8;
+
+ ast->has_pal = 1;
+ goto start_sync;
+ } else if (((ast->prefix_count < 5 || sync + 9 > i) &&
+ d[2] < 128 && d[3] < 128) ||
+ d[2] * 256 + d[3] == ast->prefix /* ||
+ (d[2] == 'd' && d[3] == 'c') ||
+ (d[2] == 'w' && d[3] == 'b') */) {
+ if (exit_early)
+ return 0;
+ if (d[2] * 256 + d[3] == ast->prefix)
+ ast->prefix_count++;
+ else {
+ ast->prefix = d[2] * 256 + d[3];
+ ast->prefix_count = 0;
+ }
+
+ avi->stream_index = n;
+ ast->packet_size = size + 8;
+ ast->remaining = size;
+
+ if (size || !ast->sample_size) {
+ uint64_t pos = avio_tell(pb) - 8;
+ if (!st->index_entries || !st->nb_index_entries ||
+ st->index_entries[st->nb_index_entries - 1].pos < pos) {
+ av_add_index_entry(st, pos, ast->frame_offset, size,
+ 0, AVINDEX_KEYFRAME);
+ }
+ }
+ return 0;
+ }