1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "media/ffmpeg/ffmpeg_common.h"
8 #include "media/filters/bitstream_converter.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 static const char kTestFilterName
[] = "test_filter";
14 static uint8_t kFailData
[] = { 3, 2, 1 };
15 static uint8_t kNewBufferData
[] = { 2, 1 };
16 static uint8_t kInPlaceData
[] = { 1 };
17 static const int kFailSize
= 3;
18 static const int kNewBufferSize
= 2;
19 static const int kInPlaceSize
= 1;
22 // Test filter function that looks for specific input data and changes it's
23 // behavior based on what is passed to |buf| & |buf_size|. The three behaviors
24 // simulated are the following:
25 // - Create a new output buffer. Triggered by |buf| == |kNewBufferData|.
26 // - Use the existing output buffer. Triggered by |buf| == |kInPlaceData|.
27 // - Signal an error. Triggered by |buf| == |kFailData|.
28 static int DoFilter(AVBitStreamFilterContext
* bsfc
,
29 AVCodecContext
* avctx
,
36 if (buf_size
== kNewBufferSize
&&
37 !memcmp(buf
, kNewBufferData
, kNewBufferSize
)) {
38 *poutbuf_size
= buf_size
+ 1;
39 *poutbuf
= static_cast<uint8
*>(av_malloc(*poutbuf_size
));
41 memcpy((*poutbuf
) + 1, buf
, buf_size
);
43 } else if (buf_size
== kInPlaceSize
&&
44 !memcmp(buf
, kInPlaceData
, kInPlaceSize
)) {
51 static void DoClose(AVBitStreamFilterContext
* bsfc
) {}
53 static AVBitStreamFilter g_stream_filter
= {
55 0, // Private Data Size
58 0, // Next filter pointer.
61 class BitstreamConverterTest
: public testing::Test
{
63 BitstreamConverterTest() {
64 memset(&test_stream_context_
, 0, sizeof(test_stream_context_
));
65 memset(&test_packet_
, 0, sizeof(test_packet_
));
66 test_packet_
.data
= kFailData
;
67 test_packet_
.size
= kFailSize
;
70 virtual ~BitstreamConverterTest() {
71 av_free_packet(&test_packet_
);
74 static void SetUpTestCase() {
75 // Register g_stream_filter if it isn't already registered.
76 if (!g_stream_filter
.next
)
77 av_register_bitstream_filter(&g_stream_filter
);
80 AVCodecContext test_stream_context_
;
81 AVPacket test_packet_
;
84 DISALLOW_COPY_AND_ASSIGN(BitstreamConverterTest
);
87 TEST_F(BitstreamConverterTest
, InitializeFailed
) {
88 FFmpegBitstreamConverter
converter("BAD_FILTER_NAME", &test_stream_context_
);
90 EXPECT_FALSE(converter
.Initialize());
93 TEST_F(BitstreamConverterTest
, InitializeSuccess
) {
94 FFmpegBitstreamConverter
converter(kTestFilterName
, &test_stream_context_
);
95 EXPECT_TRUE(converter
.Initialize());
98 TEST_F(BitstreamConverterTest
, ConvertPacket_NotInitialized
) {
99 FFmpegBitstreamConverter
converter(kTestFilterName
, &test_stream_context_
);
101 EXPECT_FALSE(converter
.ConvertPacket(&test_packet_
));
104 TEST_F(BitstreamConverterTest
, ConvertPacket_FailedFilter
) {
105 FFmpegBitstreamConverter
converter(kTestFilterName
, &test_stream_context_
);
107 EXPECT_TRUE(converter
.Initialize());
109 EXPECT_FALSE(converter
.ConvertPacket(&test_packet_
));
112 TEST_F(BitstreamConverterTest
, ConvertPacket_Success
) {
113 FFmpegBitstreamConverter
converter(kTestFilterName
, &test_stream_context_
);
115 EXPECT_TRUE(converter
.Initialize());
117 // Ensure our packet doesn't already have a destructor.
118 ASSERT_TRUE(test_packet_
.destruct
== NULL
);
120 test_packet_
.data
= kNewBufferData
;
121 test_packet_
.size
= kNewBufferSize
;
123 EXPECT_TRUE(converter
.ConvertPacket(&test_packet_
));
124 EXPECT_NE(kNewBufferData
, test_packet_
.data
);
125 EXPECT_EQ(kNewBufferSize
+ 1, test_packet_
.size
);
126 EXPECT_TRUE(test_packet_
.destruct
!= NULL
);
129 TEST_F(BitstreamConverterTest
, ConvertPacket_SuccessInPlace
) {
130 FFmpegBitstreamConverter
converter(kTestFilterName
, &test_stream_context_
);
132 EXPECT_TRUE(converter
.Initialize());
134 // Ensure our packet is in a sane start state.
135 ASSERT_TRUE(test_packet_
.destruct
== NULL
);
136 test_packet_
.data
= kInPlaceData
;
137 test_packet_
.size
= kInPlaceSize
;
139 EXPECT_TRUE(converter
.ConvertPacket(&test_packet_
));
140 EXPECT_EQ(kInPlaceData
, test_packet_
.data
);
141 EXPECT_EQ(kInPlaceSize
, test_packet_
.size
);
142 EXPECT_TRUE(test_packet_
.destruct
== NULL
);