OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/base/media_file_checker.h" | 5 #include "media/base/media_file_checker.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <map> | 9 #include <map> |
10 #include <memory> | 10 #include <memory> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "media/ffmpeg/ffmpeg_common.h" | 15 #include "media/ffmpeg/ffmpeg_common.h" |
16 #include "media/ffmpeg/ffmpeg_deleters.h" | |
16 #include "media/filters/blocking_url_protocol.h" | 17 #include "media/filters/blocking_url_protocol.h" |
17 #include "media/filters/ffmpeg_glue.h" | 18 #include "media/filters/ffmpeg_glue.h" |
18 #include "media/filters/file_data_source.h" | 19 #include "media/filters/file_data_source.h" |
19 | 20 |
20 namespace media { | 21 namespace media { |
21 | 22 |
22 static const int64_t kMaxCheckTimeInSeconds = 5; | 23 static const int64_t kMaxCheckTimeInSeconds = 5; |
23 | 24 |
24 static void OnError(bool* called) { | 25 static void OnError(bool* called) { |
25 *called = false; | 26 *called = false; |
(...skipping 11 matching lines...) Expand all Loading... | |
37 media::FFmpegGlue glue(&protocol); | 38 media::FFmpegGlue glue(&protocol); |
38 AVFormatContext* format_context = glue.format_context(); | 39 AVFormatContext* format_context = glue.format_context(); |
39 | 40 |
40 if (!glue.OpenContext()) | 41 if (!glue.OpenContext()) |
41 return false; | 42 return false; |
42 | 43 |
43 if (avformat_find_stream_info(format_context, NULL) < 0) | 44 if (avformat_find_stream_info(format_context, NULL) < 0) |
44 return false; | 45 return false; |
45 | 46 |
46 // Remember the codec context for any decodable audio or video streams. | 47 // Remember the codec context for any decodable audio or video streams. |
47 std::map<int, AVCodecContext*> stream_contexts; | 48 std::map<int, std::unique_ptr<AVCodecContext, ScopedPtrAVFreeContext>> |
49 stream_contexts; | |
48 for (size_t i = 0; i < format_context->nb_streams; ++i) { | 50 for (size_t i = 0; i < format_context->nb_streams; ++i) { |
49 AVCodecContext* c = format_context->streams[i]->codec; | 51 AVCodecParameters* cp = format_context->streams[i]->codecpar; |
50 if (c->codec_type == AVMEDIA_TYPE_AUDIO || | 52 |
51 c->codec_type == AVMEDIA_TYPE_VIDEO) { | 53 if (cp->codec_type == AVMEDIA_TYPE_AUDIO || |
52 AVCodec* codec = avcodec_find_decoder(c->codec_id); | 54 cp->codec_type == AVMEDIA_TYPE_VIDEO) { |
53 if (codec && avcodec_open2(c, codec, NULL) >= 0) | 55 std::unique_ptr<AVCodecContext, ScopedPtrAVFreeContext> c( |
54 stream_contexts[i] = c; | 56 avcodec_alloc_context3(NULL)); |
57 if (!c || avcodec_parameters_to_context(c.get(), cp) < 0) | |
DaleCurtis
2016/11/14 17:58:20
c can't ever be null.
wolenetz
2016/11/15 02:15:35
Hmm. It could if acvodec_alloc_context3 failed mal
| |
58 continue; | |
59 AVCodec* codec = avcodec_find_decoder(cp->codec_id); | |
60 if (codec && avcodec_open2(c.get(), codec, NULL) >= 0) | |
61 stream_contexts[i] = std::move(c); | |
55 } | 62 } |
56 } | 63 } |
57 | 64 |
58 if (stream_contexts.size() == 0) | 65 if (stream_contexts.size() == 0) |
59 return false; | 66 return false; |
60 | 67 |
61 AVPacket packet; | 68 AVPacket packet; |
62 std::unique_ptr<AVFrame, media::ScopedPtrAVFreeFrame> frame(av_frame_alloc()); | 69 std::unique_ptr<AVFrame, media::ScopedPtrAVFreeFrame> frame(av_frame_alloc()); |
63 int result = 0; | 70 int result = 0; |
64 | 71 |
65 const base::TimeTicks deadline = base::TimeTicks::Now() + | 72 const base::TimeTicks deadline = base::TimeTicks::Now() + |
66 std::min(check_time, | 73 std::min(check_time, |
67 base::TimeDelta::FromSeconds(kMaxCheckTimeInSeconds)); | 74 base::TimeDelta::FromSeconds(kMaxCheckTimeInSeconds)); |
68 do { | 75 do { |
69 result = av_read_frame(glue.format_context(), &packet); | 76 result = av_read_frame(glue.format_context(), &packet); |
70 if (result < 0) | 77 if (result < 0) |
71 break; | 78 break; |
72 | 79 |
73 std::map<int, AVCodecContext*>::const_iterator it = | 80 std::map<int, std::unique_ptr<AVCodecContext, ScopedPtrAVFreeContext>>:: |
74 stream_contexts.find(packet.stream_index); | 81 const_iterator it = stream_contexts.find(packet.stream_index); |
75 if (it == stream_contexts.end()) { | 82 if (it == stream_contexts.end()) { |
76 av_packet_unref(&packet); | 83 av_packet_unref(&packet); |
77 continue; | 84 continue; |
78 } | 85 } |
79 AVCodecContext* av_context = it->second; | 86 AVCodecContext* av_context = it->second.get(); |
80 | 87 |
81 int frame_decoded = 0; | 88 int frame_decoded = 0; |
82 if (av_context->codec_type == AVMEDIA_TYPE_AUDIO) { | 89 if (av_context->codec_type == AVMEDIA_TYPE_AUDIO) { |
83 // A shallow copy of packet so we can slide packet.data as frames are | 90 // A shallow copy of packet so we can slide packet.data as frames are |
84 // decoded; otherwise av_packet_unref() will corrupt memory. | 91 // decoded; otherwise av_packet_unref() will corrupt memory. |
85 AVPacket temp_packet = packet; | 92 AVPacket temp_packet = packet; |
86 do { | 93 do { |
87 result = avcodec_decode_audio4(av_context, frame.get(), &frame_decoded, | 94 result = avcodec_decode_audio4(av_context, frame.get(), &frame_decoded, |
88 &temp_packet); | 95 &temp_packet); |
89 if (result < 0) | 96 if (result < 0) |
90 break; | 97 break; |
91 av_frame_unref(frame.get()); | 98 av_frame_unref(frame.get()); |
92 temp_packet.size -= result; | 99 temp_packet.size -= result; |
93 temp_packet.data += result; | 100 temp_packet.data += result; |
94 frame_decoded = 0; | 101 frame_decoded = 0; |
95 } while (temp_packet.size > 0); | 102 } while (temp_packet.size > 0); |
96 } else if (av_context->codec_type == AVMEDIA_TYPE_VIDEO) { | 103 } else if (av_context->codec_type == AVMEDIA_TYPE_VIDEO) { |
97 result = avcodec_decode_video2(av_context, frame.get(), &frame_decoded, | 104 result = avcodec_decode_video2(av_context, frame.get(), &frame_decoded, |
98 &packet); | 105 &packet); |
99 if (result >= 0 && frame_decoded) | 106 if (result >= 0 && frame_decoded) |
100 av_frame_unref(frame.get()); | 107 av_frame_unref(frame.get()); |
101 } | 108 } |
102 av_packet_unref(&packet); | 109 av_packet_unref(&packet); |
103 } while (base::TimeTicks::Now() < deadline && read_ok && result >= 0); | 110 } while (base::TimeTicks::Now() < deadline && read_ok && result >= 0); |
104 | 111 |
112 stream_contexts.clear(); | |
105 return read_ok && (result == AVERROR_EOF || result >= 0); | 113 return read_ok && (result == AVERROR_EOF || result >= 0); |
106 } | 114 } |
107 | 115 |
108 } // namespace media | 116 } // namespace media |
OLD | NEW |