Skip to content

Commit f76d965

Browse files
yongtangterrytangyuan
authored andcommitted
Add batch support for VideoDataset and support reading from streams (#295)
* Add batch support for VideoDataset Signed-off-by: Yong Tang <[email protected]> * Support read video from stream (instead of file name). Fixes 144 Signed-off-by: Yong Tang <[email protected]> * Address review feedback Signed-off-by: Yong Tang <[email protected]>
1 parent 7ba9f20 commit f76d965

File tree

9 files changed

+195
-223
lines changed

9 files changed

+195
-223
lines changed

tensorflow_io/video/BUILD

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,70 +6,61 @@ cc_binary(
66
name = "python/ops/_video_ops_ffmpeg_3.4.so",
77
srcs = [
88
"kernels/ffmpeg.cc",
9-
"kernels/video_dataset_ops.cc",
9+
"kernels/video_input.cc",
1010
"kernels/video_reader.h",
11-
"ops/dataset_ops.cc",
11+
"ops/video_ops.cc",
1212
],
1313
copts = [
1414
"-pthread",
1515
"-std=c++11",
1616
"-DNDEBUG",
1717
],
18-
data = [],
1918
includes = ["."],
20-
linkopts = [],
2119
linkshared = 1,
2220
deps = [
21+
"//tensorflow_io/core:dataset_ops",
2322
"@ffmpeg_3_4//:ffmpeg",
24-
"@local_config_tf//:libtensorflow_framework",
25-
"@local_config_tf//:tf_header_lib",
2623
],
2724
)
2825

2926
cc_binary(
3027
name = "python/ops/_video_ops_ffmpeg_2.8.so",
3128
srcs = [
3229
"kernels/ffmpeg.cc",
33-
"kernels/video_dataset_ops.cc",
30+
"kernels/video_input.cc",
3431
"kernels/video_reader.h",
35-
"ops/dataset_ops.cc",
32+
"ops/video_ops.cc",
3633
],
3734
copts = [
3835
"-pthread",
3936
"-std=c++11",
4037
"-DNDEBUG",
4138
],
42-
data = [],
4339
includes = ["."],
44-
linkopts = [],
4540
linkshared = 1,
4641
deps = [
42+
"//tensorflow_io/core:dataset_ops",
4743
"@ffmpeg_2_8//:ffmpeg",
48-
"@local_config_tf//:libtensorflow_framework",
49-
"@local_config_tf//:tf_header_lib",
5044
],
5145
)
5246

5347
cc_binary(
5448
name = "python/ops/_video_ops_libav_9.20.so",
5549
srcs = [
5650
"kernels/ffmpeg.cc",
57-
"kernels/video_dataset_ops.cc",
51+
"kernels/video_input.cc",
5852
"kernels/video_reader.h",
59-
"ops/dataset_ops.cc",
53+
"ops/video_ops.cc",
6054
],
6155
copts = [
6256
"-pthread",
6357
"-std=c++11",
6458
"-DNDEBUG",
6559
],
66-
data = [],
6760
includes = ["."],
68-
linkopts = [],
6961
linkshared = 1,
7062
deps = [
63+
"//tensorflow_io/core:dataset_ops",
7164
"@libav_9_20//:libav",
72-
"@local_config_tf//:libtensorflow_framework",
73-
"@local_config_tf//:tf_header_lib",
7465
],
7566
)

tensorflow_io/video/kernels/ffmpeg.cc

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,63 @@ namespace tensorflow {
3434
namespace data {
3535
namespace video {
3636

37+
static int io_read_packet(void *opaque, uint8_t *buf, int buf_size) {
38+
VideoReader *r = (VideoReader *)opaque;
39+
StringPiece result;
40+
Status status = r->stream_->Read(r->offset_, buf_size, &result, (char *)buf);
41+
if (!(status.ok() || errors::IsOutOfRange(status))) {
42+
return -1;
43+
}
44+
r->offset_ += result.size();
45+
return result.size();
46+
}
47+
48+
static int64_t io_seek(void *opaque, int64_t offset, int whence) {
49+
VideoReader *r = (VideoReader *)opaque;
50+
uint64 file_size = 0;
51+
Status status = r->stream_->GetFileSize(&file_size);
52+
if (!status.ok()) {
53+
return -1;
54+
}
55+
switch (whence)
56+
{
57+
case SEEK_SET:
58+
if (offset > file_size) {
59+
return -1;
60+
}
61+
r->offset_ = offset;
62+
return r->offset_;
63+
case SEEK_CUR:
64+
if (r->offset_ + offset > file_size) {
65+
return -1;
66+
}
67+
r->offset_ += offset;
68+
return r->offset_;
69+
case SEEK_END:
70+
if (offset > file_size) {
71+
return -1;
72+
}
73+
r->offset_ = file_size - offset;
74+
return r->offset_;
75+
case AVSEEK_SIZE:
76+
return file_size;
77+
default:
78+
break;
79+
}
80+
return -1;
81+
}
82+
3783
Status VideoReader::ReadHeader()
3884
{
85+
// Allocate format
86+
if ((format_context_ = avformat_alloc_context()) == NULL) {
87+
return errors::InvalidArgument("could not allocate format context");
88+
}
89+
// Allocate context
90+
if ((io_context_ = avio_alloc_context(NULL, 0, 0, this, io_read_packet, NULL, io_seek)) == NULL) {
91+
return errors::InvalidArgument("could not allocate io context");
92+
}
93+
format_context_->pb = io_context_;
3994
// Open input file, and allocate format context
4095
if (avformat_open_input(&format_context_, filename_.c_str(), NULL, NULL) < 0) {
4196
return errors::InvalidArgument("could not open video file: ", filename_);
@@ -206,6 +261,10 @@ VideoReader::~VideoReader() {
206261
avcodec_free_context(&codec_context_);
207262
#endif
208263
avformat_close_input(&format_context_);
264+
av_free(format_context_);
265+
if (io_context_ != NULL) {
266+
av_free(io_context_);
267+
}
209268
}
210269

211270
} // namespace

tensorflow_io/video/kernels/video_dataset_ops.cc

Lines changed: 0 additions & 187 deletions
This file was deleted.

0 commit comments

Comments
 (0)