00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include <sys/poll.h>
00027 #include <libraw1394/raw1394.h>
00028 #include <libavc1394/avc1394.h>
00029 #include <libavc1394/rom1394.h>
00030 #include <libiec61883/iec61883.h>
00031 #include "libavformat/dv.h"
00032 #include "libavformat/mpegts.h"
00033 #include "libavutil/opt.h"
00034 #include "avdevice.h"
00035
00036 #define THREADS HAVE_PTHREADS
00037
00038 #if THREADS
00039 #include <pthread.h>
00040 #endif
00041
00042 #define MOTDCT_SPEC_ID 0x00005068
00043 #define IEC61883_AUTO 0
00044 #define IEC61883_DV 1
00045 #define IEC61883_HDV 2
00046
00052 typedef struct DVPacket {
00053 uint8_t *buf;
00054 int len;
00055 struct DVPacket *next;
00056 } DVPacket;
00057
00058 struct iec61883_data {
00059 AVClass *class;
00060 raw1394handle_t raw1394;
00061 iec61883_dv_fb_t iec61883_dv;
00062 iec61883_mpeg2_t iec61883_mpeg2;
00063
00064 DVDemuxContext *dv_demux;
00065 MpegTSContext *mpeg_demux;
00066
00067 DVPacket *queue_first;
00068 DVPacket *queue_last;
00069
00070 char *device_guid;
00071
00072 int packets;
00073 int max_packets;
00074
00075 int bandwidth;
00076 int channel;
00077 int input_port;
00078 int type;
00079 int node;
00080 int output_port;
00081 int thread_loop;
00082 int receiving;
00083 int receive_error;
00084 int eof;
00085
00086 struct pollfd raw1394_poll;
00087
00089 int (*parse_queue)(struct iec61883_data *dv, AVPacket *pkt);
00090
00091 #if THREADS
00092 pthread_t receive_task_thread;
00093 pthread_mutex_t mutex;
00094 pthread_cond_t cond;
00095 #endif
00096 };
00097
00098 static int iec61883_callback(unsigned char *data, int length,
00099 int complete, void *callback_data)
00100 {
00101 struct iec61883_data *dv = callback_data;
00102 DVPacket *packet;
00103 int ret;
00104
00105 #ifdef THREADS
00106 pthread_mutex_lock(&dv->mutex);
00107 #endif
00108
00109 if (dv->packets >= dv->max_packets) {
00110 av_log(NULL, AV_LOG_ERROR, "DV packet queue overrun, dropping.\n");
00111 ret = 0;
00112 goto exit;
00113 }
00114
00115 packet = av_mallocz(sizeof(*packet));
00116 if (!packet) {
00117 ret = -1;
00118 goto exit;
00119 }
00120
00121 packet->buf = av_malloc(length);
00122 if (!packet->buf) {
00123 ret = -1;
00124 goto exit;
00125 }
00126 packet->len = length;
00127
00128 memcpy(packet->buf, data, length);
00129
00130 if (dv->queue_first) {
00131 dv->queue_last->next = packet;
00132 dv->queue_last = packet;
00133 } else {
00134 dv->queue_first = packet;
00135 dv->queue_last = packet;
00136 }
00137 dv->packets++;
00138
00139 ret = 0;
00140
00141 exit:
00142 #ifdef THREADS
00143 pthread_cond_broadcast(&dv->cond);
00144 pthread_mutex_unlock(&dv->mutex);
00145 #endif
00146 return ret;
00147 }
00148
00149 static void *iec61883_receive_task(void *opaque)
00150 {
00151 struct iec61883_data *dv = (struct iec61883_data *)opaque;
00152 int result;
00153
00154 #ifdef THREADS
00155 while (dv->thread_loop)
00156 #endif
00157 {
00158 while ((result = poll(&dv->raw1394_poll, 1, 200)) < 0) {
00159 if (!(errno == EAGAIN || errno == EINTR)) {
00160 av_log(NULL, AV_LOG_ERROR, "Raw1394 poll error occurred.\n");
00161 dv->receive_error = AVERROR(EIO);
00162 return NULL;
00163 }
00164 }
00165 if (result > 0 && ((dv->raw1394_poll.revents & POLLIN)
00166 || (dv->raw1394_poll.revents & POLLPRI))) {
00167 dv->receiving = 1;
00168 raw1394_loop_iterate(dv->raw1394);
00169 } else if (dv->receiving) {
00170 av_log(NULL, AV_LOG_ERROR, "No more input data available\n");
00171 #ifdef THREADS
00172 pthread_mutex_lock(&dv->mutex);
00173 dv->eof = 1;
00174 pthread_cond_broadcast(&dv->cond);
00175 pthread_mutex_unlock(&dv->mutex);
00176 #else
00177 dv->eof = 1;
00178 #endif
00179 return NULL;
00180 }
00181 }
00182
00183 return NULL;
00184 }
00185
00186 static int iec61883_parse_queue_dv(struct iec61883_data *dv, AVPacket *pkt)
00187 {
00188 DVPacket *packet;
00189 int size;
00190
00191 size = avpriv_dv_get_packet(dv->dv_demux, pkt);
00192 if (size > 0)
00193 return size;
00194
00195 packet = dv->queue_first;
00196 if (!packet)
00197 return -1;
00198
00199 size = avpriv_dv_produce_packet(dv->dv_demux, pkt,
00200 packet->buf, packet->len, -1);
00201 pkt->destruct = av_destruct_packet;
00202 dv->queue_first = packet->next;
00203 av_free(packet);
00204 dv->packets--;
00205
00206 if (size > 0)
00207 return size;
00208
00209 return -1;
00210 }
00211
00212 static int iec61883_parse_queue_hdv(struct iec61883_data *dv, AVPacket *pkt)
00213 {
00214 DVPacket *packet;
00215 int size;
00216
00217 while (dv->queue_first) {
00218 packet = dv->queue_first;
00219 size = ff_mpegts_parse_packet(dv->mpeg_demux, pkt, packet->buf,
00220 packet->len);
00221 dv->queue_first = packet->next;
00222 av_free(packet->buf);
00223 av_free(packet);
00224 dv->packets--;
00225
00226 if (size > 0)
00227 return size;
00228 }
00229
00230 return -1;
00231 }
00232
00233 static int iec61883_read_header(AVFormatContext *context)
00234 {
00235 struct iec61883_data *dv = context->priv_data;
00236 struct raw1394_portinfo pinf[16];
00237 rom1394_directory rom_dir;
00238 char *endptr;
00239 int inport;
00240 int nb_ports;
00241 int port = -1;
00242 int response;
00243 int i, j = 0;
00244 uint64_t guid = 0;
00245
00246 dv->input_port = -1;
00247 dv->output_port = -1;
00248 dv->channel = -1;
00249
00250 dv->raw1394 = raw1394_new_handle();
00251
00252 if (!dv->raw1394) {
00253 av_log(context, AV_LOG_ERROR, "Failed to open IEEE1394 interface.\n");
00254 return AVERROR(EIO);
00255 }
00256
00257 if ((nb_ports = raw1394_get_port_info(dv->raw1394, pinf, 16)) < 0) {
00258 av_log(context, AV_LOG_ERROR, "Failed to get number of IEEE1394 ports.\n");
00259 goto fail;
00260 }
00261
00262 inport = strtol(context->filename, &endptr, 10);
00263 if (endptr != context->filename && *endptr == '\0') {
00264 av_log(context, AV_LOG_INFO, "Selecting IEEE1394 port: %d\n", inport);
00265 j = inport;
00266 nb_ports = inport + 1;
00267 } else if (strcmp(context->filename, "auto")) {
00268 av_log(context, AV_LOG_ERROR, "Invalid input \"%s\", you should specify "
00269 "\"auto\" for auto-detection, or the port number.\n", context->filename);
00270 goto fail;
00271 }
00272
00273 if (dv->device_guid) {
00274 if (sscanf(dv->device_guid, "%llx", (long long unsigned int *)&guid) != 1) {
00275 av_log(context, AV_LOG_INFO, "Invalid dvguid parameter: %s\n",
00276 dv->device_guid);
00277 goto fail;
00278 }
00279 }
00280
00281 for (; j < nb_ports && port==-1; ++j) {
00282 raw1394_destroy_handle(dv->raw1394);
00283
00284 if (!(dv->raw1394 = raw1394_new_handle_on_port(j))) {
00285 av_log(context, AV_LOG_ERROR, "Failed setting IEEE1394 port.\n");
00286 goto fail;
00287 }
00288
00289 for (i=0; i<raw1394_get_nodecount(dv->raw1394); ++i) {
00290
00291
00292
00293 if (guid > 1) {
00294 if (guid == rom1394_get_guid(dv->raw1394, i)) {
00295 dv->node = i;
00296 port = j;
00297 break;
00298 }
00299 } else {
00300
00301
00302
00303 if (rom1394_get_directory(dv->raw1394, i, &rom_dir) < 0)
00304 continue;
00305 if (((rom1394_get_node_type(&rom_dir) == ROM1394_NODE_TYPE_AVC) &&
00306 avc1394_check_subunit_type(dv->raw1394, i, AVC1394_SUBUNIT_TYPE_VCR)) ||
00307 (rom_dir.unit_spec_id == MOTDCT_SPEC_ID)) {
00308 rom1394_free_directory(&rom_dir);
00309 dv->node = i;
00310 port = j;
00311 break;
00312 }
00313 rom1394_free_directory(&rom_dir);
00314 }
00315 }
00316 }
00317
00318 if (port == -1) {
00319 av_log(context, AV_LOG_ERROR, "No AV/C devices found.\n");
00320 goto fail;
00321 }
00322
00323
00324
00325 iec61883_cmp_normalize_output(dv->raw1394, 0xffc0 | dv->node);
00326
00327
00328
00329 if (dv->type == IEC61883_AUTO) {
00330 response = avc1394_transaction(dv->raw1394, dv->node,
00331 AVC1394_CTYPE_STATUS |
00332 AVC1394_SUBUNIT_TYPE_TAPE_RECORDER |
00333 AVC1394_SUBUNIT_ID_0 |
00334 AVC1394_VCR_COMMAND_OUTPUT_SIGNAL_MODE |
00335 0xFF, 2);
00336 response = AVC1394_GET_OPERAND0(response);
00337 dv->type = (response == 0x10 || response == 0x90 || response == 0x1A || response == 0x9A) ?
00338 IEC61883_HDV : IEC61883_DV;
00339 }
00340
00341
00342
00343 dv->channel = iec61883_cmp_connect(dv->raw1394, dv->node, &dv->output_port,
00344 raw1394_get_local_id(dv->raw1394),
00345 &dv->input_port, &dv->bandwidth);
00346
00347 if (dv->channel < 0)
00348 dv->channel = 63;
00349
00350 if (!dv->max_packets)
00351 dv->max_packets = 100;
00352
00353 if (dv->type == IEC61883_HDV) {
00354
00355
00356
00357 avformat_new_stream(context, NULL);
00358
00359 dv->mpeg_demux = ff_mpegts_parse_open(context);
00360 if (!dv->mpeg_demux)
00361 goto fail;
00362
00363 dv->parse_queue = iec61883_parse_queue_hdv;
00364
00365 dv->iec61883_mpeg2 = iec61883_mpeg2_recv_init(dv->raw1394,
00366 (iec61883_mpeg2_recv_t)iec61883_callback,
00367 dv);
00368
00369 dv->max_packets *= 766;
00370 } else {
00371
00372
00373
00374 dv->dv_demux = avpriv_dv_init_demux(context);
00375 if (!dv->dv_demux)
00376 goto fail;
00377
00378 dv->parse_queue = iec61883_parse_queue_dv;
00379
00380 dv->iec61883_dv = iec61883_dv_fb_init(dv->raw1394, iec61883_callback, dv);
00381 }
00382
00383 dv->raw1394_poll.fd = raw1394_get_fd(dv->raw1394);
00384 dv->raw1394_poll.events = POLLIN | POLLERR | POLLHUP | POLLPRI;
00385
00386
00387
00388 if (dv->type == IEC61883_HDV)
00389 iec61883_mpeg2_recv_start(dv->iec61883_mpeg2, dv->channel);
00390 else
00391 iec61883_dv_fb_start(dv->iec61883_dv, dv->channel);
00392
00393 #if THREADS
00394 dv->thread_loop = 1;
00395 pthread_mutex_init(&dv->mutex, NULL);
00396 pthread_cond_init(&dv->cond, NULL);
00397 pthread_create(&dv->receive_task_thread, NULL, iec61883_receive_task, dv);
00398 #endif
00399
00400 return 0;
00401
00402 fail:
00403 raw1394_destroy_handle(dv->raw1394);
00404 return AVERROR(EIO);
00405 }
00406
00407 static int iec61883_read_packet(AVFormatContext *context, AVPacket *pkt)
00408 {
00409 struct iec61883_data *dv = context->priv_data;
00410 int size;
00411
00416 #ifdef THREADS
00417 pthread_mutex_lock(&dv->mutex);
00418 while ((size = dv->parse_queue(dv, pkt)) == -1)
00419 if (!dv->eof)
00420 pthread_cond_wait(&dv->cond, &dv->mutex);
00421 else
00422 break;
00423 pthread_mutex_unlock(&dv->mutex);
00424 #else
00425 int result;
00426 while ((size = dv->parse_queue(dv, pkt)) == -1) {
00427 iec61883_receive_task((void *)dv);
00428 if (dv->receive_error)
00429 return dv->receive_error;
00430 }
00431 #endif
00432
00433 return size;
00434 }
00435
00436 static int iec61883_close(AVFormatContext *context)
00437 {
00438 struct iec61883_data *dv = context->priv_data;
00439
00440 #if THREADS
00441 dv->thread_loop = 0;
00442 pthread_join(dv->receive_task_thread, NULL);
00443 pthread_cond_destroy(&dv->cond);
00444 pthread_mutex_destroy(&dv->mutex);
00445 #endif
00446
00447 if (dv->type == IEC61883_HDV) {
00448 iec61883_mpeg2_recv_stop(dv->iec61883_mpeg2);
00449 iec61883_mpeg2_close(dv->iec61883_mpeg2);
00450 ff_mpegts_parse_close(dv->mpeg_demux);
00451 } else {
00452 iec61883_dv_fb_stop(dv->iec61883_dv);
00453 iec61883_dv_fb_close(dv->iec61883_dv);
00454 }
00455 while (dv->queue_first) {
00456 DVPacket *packet = dv->queue_first;
00457 dv->queue_first = packet->next;
00458 av_free(packet->buf);
00459 av_free(packet);
00460 }
00461
00462 iec61883_cmp_disconnect(dv->raw1394, dv->node, dv->output_port,
00463 raw1394_get_local_id(dv->raw1394),
00464 dv->input_port, dv->channel, dv->bandwidth);
00465
00466 raw1394_destroy_handle(dv->raw1394);
00467
00468 return 0;
00469 }
00470
00471 static const AVOption options[] = {
00472 { "dvtype", "override autodetection of DV/HDV", offsetof(struct iec61883_data, type), AV_OPT_TYPE_INT, {.i64 = IEC61883_AUTO}, IEC61883_AUTO, IEC61883_HDV, AV_OPT_FLAG_DECODING_PARAM, "dvtype" },
00473 { "auto", "auto detect DV/HDV", 0, AV_OPT_TYPE_CONST, {.i64 = IEC61883_AUTO}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "dvtype" },
00474 { "dv", "force device being treated as DV device", 0, AV_OPT_TYPE_CONST, {.i64 = IEC61883_DV}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "dvtype" },
00475 { "hdv" , "force device being treated as HDV device", 0, AV_OPT_TYPE_CONST, {.i64 = IEC61883_HDV}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "dvtype" },
00476 { "dvbuffer", "set queue buffer size (in packets)", offsetof(struct iec61883_data, max_packets), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
00477 { "dvguid", "select one of multiple DV devices by its GUID", offsetof(struct iec61883_data, device_guid), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
00478 { NULL },
00479 };
00480
00481 static const AVClass iec61883_class = {
00482 .class_name = "iec61883 indev",
00483 .item_name = av_default_item_name,
00484 .option = options,
00485 .version = LIBAVUTIL_VERSION_INT,
00486 };
00487
00488 AVInputFormat ff_iec61883_demuxer = {
00489 .name = "iec61883",
00490 .long_name = NULL_IF_CONFIG_SMALL("libiec61883 (new DV1394) A/V input device"),
00491 .priv_data_size = sizeof(struct iec61883_data),
00492 .read_header = iec61883_read_header,
00493 .read_packet = iec61883_read_packet,
00494 .read_close = iec61883_close,
00495 .flags = AVFMT_NOFILE,
00496 .priv_class = &iec61883_class,
00497 };